PY-101 ยท Loops
for loops and range()
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
A for loop runs a block once for each item in a sequence, such as a list, a string or a range of numbers. range(5) gives 0 to 4, and range(start, stop, step) lets you choose where to start, where to stop and how far to jump. The stop number is never included.
- Loop over a list or string
- Count with range()
- Use start, stop and step
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
fruits = ["apple", "mango", "kiwi"]for fruit in fruits:print("I like", fruit)for n in range(1, 6, 2):print(n)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
I like apple
I like mango
I like kiwi
1
3
5 ๐ฏ Print the 7 times table from 7 x 1 to 7 x 5, one line each, like: 7 x 1 = 7
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
for i in range(1, 6): print(f"{n} x {i} = {n * i}")
- The problemYou need to send 30 reminder emails. Writing 30 print lines is silly. A loop writes one and repeats it 30 times.
- The Python idea
for item in sequence:A for loop runs a block once for each item in a sequence, such as a list, a string or a range of numbers. - In real lifeA payroll script loops over every employee and works out each salary. One loop, any number of employees.
Start with a question
You need to send 30 reminder emails. Writing 30 print lines is silly. A loop writes one and repeats it 30 times.
Computers are great at repeating work. Every report, every list of results and every game frame uses loops.
Explain it like I'm 10
A loop is like a teacher calling the register: the same question for every name on the list, one at a time, until the list ends.
The technical explanation
for name in names: takes each item in turn and runs the indented block with name set to it.
range(5) produces 0, 1, 2, 3, 4. range(2, 6) produces 2 to 5. range(0, 10, 2) counts in twos.
A string is a sequence too: for ch in "hi": loops over each character.
Syntax
for item in sequence:
...
for i in range(start, stop, step):
... Real-world scenario
A payroll script loops over every employee and works out each salary. One loop, any number of employees.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: while loops โQuestions people ask
How do I loop with an index in Python?
Use enumerate: for i, item in enumerate(items): gives you the position and the item together. It is clearer than range(len(items)).
Can range count backwards?
Yes, with a negative step: range(5, 0, -1) gives 5, 4, 3, 2, 1.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner How do I loop with an index in Python?
Use enumerate: for i, item in enumerate(items): gives you the position and the item together. It is clearer than range(len(items)).
beginner Can range count backwards?
Yes, with a negative step: range(5, 0, -1) gives 5, 4, 3, 2, 1.
intermediate What does range(4) produce?
0, 1, 2, 3. It starts at 0 and stops before 4.
intermediate What does range(2, 10, 3) produce?
2, 5, 8. Start at 2, add 3 each time, stop before 10.
Quick revision
Remember
- Loop over a list or string
- Count with range()
- Use start, stop and step
range(1, 10)stops at 9. If you want 1 to 10, writerange(1, 11).
Syntax
for item in sequence:
...
for i in range(start, stop, step):
... Official documentation
- for statements (docs.python.org)
- range() (docs.python.org)