Category Archives: Python

Python snippet: for…else

Python has a clever for…else (albeit ancient by now) construct allowing the use of an else suite. Consider the following example, written without the for…else construct: contains_even = False for element in [1, 2, 3, 4, 5]: if element % 2 == 0: contains_even = True break if contains_even: print “List contains an even element.” else: print [...]

Also posted in Development | 2 Comments