https://docs.python.org/3/tutorial/controlflow.html
The 'for' statement is used to iterate over the elements of an iterable object.
# Syntax. for item in iterable: statements # item is processed if condition1: break # optional, terminate the loop without executing 'else' statements if condition2: continue # optional, go to the next item from iterable statements else: # optional, terminate the loop statements # executed if 'break' was not used, even for an empty iterable # Warning: the name 'item' is created only if 'iterable' is not empty! # Range-based for loop in C++ (since C++11) # for (auto item : container) statement # for (auto& item : container) statement
word = "Python" for char in word: # use decriptive names if char == "o": break # characters printed: 'P', 'y', 't', 'h' print(char) assert char == "o" # 'char' is alive
fruits = ["apple", "pear", "plum", "apricot"] # use plural when possible for fruit in fruits: if fruit[0] == "a": continue # skipping "apple" and "apricot" print(fruit) assert fruit == "apricot" # What if we need an index? for i in range(len(fruits)): # C style print("{} {}".format(i, fruits[i])) # enumerate(iterable, start=0), return an enumerate object (iterator) for (i, fruit) in enumerate(fruits): # recommended print("{} {}".format(i, fruit)) [c*i for (i, c) in enumerate("abcd")] # ["", "b", "cc", "ddd"] [c*i for (i, c) in enumerate("abcd", start=1)] # ["a", "bb", "ccc", "dddd"]
# loop over pairs for t in [(1, "one"), (2, "two"), (3, "three")]: print("first {} second {}".format(t[0], t[1])) #print("first {0[0]} second {0[1]}".format(t)) # better version with unpacking for (a, b) in [(1, "one"), (2, "two"), (3, "three")]: print("first {} second {}".format(a, b))
# Code that modifies a collection while iterating over that same collection # can be tricky to get right. # It is safe to extend the list (append at the end). # It is not safe to add or remove an item in the middle of the list. L = [0, 1, 2, 3, 4, 5] for i in L: print(i) if (i % 2) == 1: # if i is odd then extend the list L.append(2 * i) # adding two even numbers L.append(4 * i)
L = [0, 1, 2, 3, 4, 5] for i in L[:]: # use copy in the loop [or list(L)] if (i % 2) == 1: L.remove(i) # changing the original list # Better solution (filtering). new_list = [i for i in L if (i % 2) != 1]
# Looping over a sequence in reverse. for char in reversed("qwerty"): # 'reversed object' print(char) # y, t, r, e, w, q
# Looping over a sequence in sorted order. for char in sorted("qwerty"): # ['e', 'q', 'r', 't', 'w', 'y'] print(char)
# Looping over two or more sequences at the same time. letters = ["a", "b", "c", "d"] numbers = [10, 20, 30, 40] for (letter, number) in zip(letters, numbers): print("{} {}".format(letter, number))
# Eliminating duplicate elements. for char in set("abracadabra"): # 'a', 'c', 'd', 'b', 'r' print(char)
# Using all() and any(). L = [1, 2, 4, 5] if all(x % 2 == 0 for x in L): print("all numbers are even") if any(x % 2 == 0 for x in L): print("there is an even number on the list")