Loop
Code that repeats, either over items (for) or while a condition holds (while).
A loop runs the same block of code more than once. A for loop repeats once per item in a sequence (a list, string, range, and so on), binding each item to a variable in turn. A while loop keeps going as long as a condition stays True. Inside either, break stops the loop early and continue skips to the next round.
for n in [1, 2, 3]: # once per item
print(n)
count = 0
while count < 2: # repeat while the condition holds
count += 1
print("done", count)
Output
1 2 3 done 2
Where this shows up in real Python
Loops do the repetitive work: processing each file, row, or item; retrying until something succeeds; or building a list one piece at a time.
Commonly used Loop tools
range(n)— loop a fixed number of timesenumerate(seq)— loop with an indexzip(a, b)— loop over two sequences togetherbreak / continue— stop early / skip to the next item[x for x in seq]— a comprehension — a loop that builds a list
Official documentation: Python Tutorial: for Statements