A list of three numbers [10, 20, 30]. You want to print each one doubled. Without a loop, what does that look like?
print(numbers[0] * 2), print(numbers[1] * 2), print(numbers[2] * 2). Three lines that are almost identical.
And if the list grows to a hundred items, you write a hundred lines? That's where loops come in. for says "do this once for each item in this list":
for n in numbers:
print(n * 2)Three lines for three items, three lines for a hundred items, three lines for a million. The loop body runs once per item; n takes the value of the current item each time.
The colon, the indent — same as if?
Identical syntax. for variable in iterable: then an indented block. Python's structure is the same wherever blocks appear.
The variable n — does it have to be that letter?
Anything legal. for item in numbers:, for x in numbers:, for value in numbers: — pick a name that makes the loop body readable. Single letters are fine for short bodies; descriptive names help when the body is longer.
Can I loop over a string? Or only lists?
Anything iterable. Strings yield characters: for c in "hello" runs five times, with c being 'h', 'e', 'l', 'l', 'o'. You'll meet more iterables (dicts, ranges, generators) as you go.
for loops — iterate over a sequenceThe shape:
for item in iterable:
# body — runs once per itemitem is a name you choose; it takes the value of each element in turniterable is anything Python can step through one item at a time — list, string, dict, range, ...ifnames = ["Ada", "Bob", "Cleo"]
for name in names:
print(f"hello {name}")
# hello Ada
# hello Bob
# hello Cleofor c in "hi":
print(c)
# h
# iStrings iterate character by character.
range(n) — N-many iterationsWhen you want a loop that runs N times without a list to iterate, range(n) produces 0, 1, ..., n-1:
for i in range(5):
print(i)
# 0
# 1
# 2
# 3
# 4range(start, stop) and range(start, stop, step) exist for finer control.
ifLoops and conditionals nest naturally:
for n in [1, 2, 3, 4, 5]:
if n % 2 == 0:
print(f"{n} is even")Indentation makes the structure visible at a glance.
for item in items:
items.append(...) # don't do this — undefined behaviourIf you need to build a new list while iterating, use a fresh list:
result = []
for item in items:
result.append(transform(item))A list of three numbers [10, 20, 30]. You want to print each one doubled. Without a loop, what does that look like?
print(numbers[0] * 2), print(numbers[1] * 2), print(numbers[2] * 2). Three lines that are almost identical.
And if the list grows to a hundred items, you write a hundred lines? That's where loops come in. for says "do this once for each item in this list":
for n in numbers:
print(n * 2)Three lines for three items, three lines for a hundred items, three lines for a million. The loop body runs once per item; n takes the value of the current item each time.
The colon, the indent — same as if?
Identical syntax. for variable in iterable: then an indented block. Python's structure is the same wherever blocks appear.
The variable n — does it have to be that letter?
Anything legal. for item in numbers:, for x in numbers:, for value in numbers: — pick a name that makes the loop body readable. Single letters are fine for short bodies; descriptive names help when the body is longer.
Can I loop over a string? Or only lists?
Anything iterable. Strings yield characters: for c in "hello" runs five times, with c being 'h', 'e', 'l', 'l', 'o'. You'll meet more iterables (dicts, ranges, generators) as you go.
for loops — iterate over a sequenceThe shape:
for item in iterable:
# body — runs once per itemitem is a name you choose; it takes the value of each element in turniterable is anything Python can step through one item at a time — list, string, dict, range, ...ifnames = ["Ada", "Bob", "Cleo"]
for name in names:
print(f"hello {name}")
# hello Ada
# hello Bob
# hello Cleofor c in "hi":
print(c)
# h
# iStrings iterate character by character.
range(n) — N-many iterationsWhen you want a loop that runs N times without a list to iterate, range(n) produces 0, 1, ..., n-1:
for i in range(5):
print(i)
# 0
# 1
# 2
# 3
# 4range(start, stop) and range(start, stop, step) exist for finer control.
ifLoops and conditionals nest naturally:
for n in [1, 2, 3, 4, 5]:
if n % 2 == 0:
print(f"{n} is even")Indentation makes the structure visible at a glance.
for item in items:
items.append(...) # don't do this — undefined behaviourIf you need to build a new list while iterating, use a fresh list:
result = []
for item in items:
result.append(transform(item))Create a free account to get started. Paid plans unlock all tracks.