for Loops
Repetition means asking Python to perform a set of instructions for several values. A for loop takes one value at a time from a sequence such as range(), text, or a list.
By the end of this lesson, you can:
- Read loop syntax and indentation
- Use all three range forms
- Visit text and list items
- Filter values inside a loop
for Loops lesson progress0%
Course progress: 0% · 0 of 16 steps completed in this module
Learn · Repeat an indented instruction
Repeat an indented instruction
A for loop takes one value at a time. The temporary name after for is the loop variable; it receives the next value each repetition. The colon begins the indented loop body.
Pattern
for number in range(5):
print(number)Code
for number in range(5):
print(number)Output
0 1 2 3 4
Why it works
- for starts the loop and number is its changing loop variable.
- range(5) supplies 0, 1, 2, 3, 4.
- The colon starts the block; the indented print runs for each value.
Remember: range(5) does not include 5. The stop value is excluded.
Common mistake
Not this
for number in range(5)
print(number)Try this
for number in range(5):
print(number)A missing colon or indentation prevents the loop body from running correctly.