June 2025
Beginner to intermediate
473 pages
13h 30m
English
In Python, loops are primarily formed using for var in elements. The loop variable accepts each of the specified elements in sequence.
for var in mylst: statements
The var loop variable remains available after the end of the loop and then contains the last assigned value:
for i in [7, 12, 3]: statementsprint(i) # Output 3
Alternatively, loops can be formulated using while. The indented statements are then executed as long as the condition is met.
while condition: statements
break terminates for and while loops prematurely:
for var in elements: statement1 if condition: break # abort the loop statement2
continue skips the remaining statements for the current loop pass but then ...
Read now
Unlock full access