Loops

while loop

The while loop is executed as long as the condition (here: x > y) is true:

1>>> x, y = 6, 3
2>>> while x > y:
3...     x -= 1
4...     if x == 4:
5...         break
6...     print(x)
7...
85
Line 1

This is a shorthand notation where x is given the value 6 and y is given the value 3.

Lines 2–10

This is the while loop with the statement x > y, which is true as long as x is greater than y.

Line 3

x is reduced by 1.

Line 4

if condition where x is to be exactly 4.

Line 5

break ends the loop.

Lines 8 and 9

outputs the results of the while loop before execution was interrupted with break.

1>>> x, y = 6, 3
2>>> while x > y:
3...     x -= 1
4...     if x == 4:
5...         continue
6...     print(x)
7...
85
93
Line 5

continue terminates the current iteration of the loop.

for loop

The for loop is simple but powerful because it can iterate over any iterable type, such as a list or a tuple. Unlike many other languages, the for loop in Python iterates over every element in a sequence for example a list or a tuple), which makes it more like a foreach loop. The following loop uses the Modulo operator % as a condition for the first occurrence of an integer divisible by 5:

 1>>> items = [1, "fünf", 5.0, 10, 11, 15]
 2>>> d = 5
 3>>> for i in items:
 4...     if not isinstance(i, int):
 5...         continue
 6...     if not i % d:
 7...         print(f"First integer found that is divisible by {d}: {i}")
 8...         break
 9...
10First integer found that is divisible by 5: 10

x is assigned each value in the list in turn. If x is not an integer, the remainder of this iteration is aborted by the continue statement. The flow control is continued with x being set to the next entry in the list. After the first matching integer is found, the loop is terminated with the break statement.

Loops with an index

You can also output the index in a for loop, for example with enumerate():

>>> data_types = ["Data types", "Numbers", "Lists"]
>>> for index, title in enumerate(data_types):
...     print(index, title)
...
0 Data types
1 Numbers
2 Lists

List Comprehensions

A list is usually generated as follows:

>>> squares = []
>>> for i in range(8):
...     squares.append(i ** 2)
...
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49]

Instead of creating an empty list and inserting each element at the end, with list comprehensions you simply define the list and its content at the same time with just a single line of code:

>>> squares = [i ** 2 for i in range(8)]
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49]

The general format for this is:

NEW_LIST = [EXPRESSION for MEMBER in ITERABLE]

Each list comprehension in Python contains three elements:

EXPRESSION

is a call to a method or another valid expression that returns a value. In the example above, the expression i ** 2 is the square of the respective member value.

MEMBER

is the object or the value in an ITERABLE. In the example above, the value is i.

ITERABLE

is a list, a set, a generator or another object that can return its elements individually. In the example above, the iterable is range(8).

You can also use optional conditions with list comprehensions, which are usually appended to the end of the expression:

>>> squares = [i ** 2 for i in range(8) if i >= 4]
>>> squares
[16, 25, 36, 49]