Nearby lessons
29 of 108Python - range() Function
What Is range()?
The range() function generates a sequence of numbers.
It is commonly used with for loops.
Forms
range(stop)range(start, stop)range(start, stop, step)
Rules
- Start value is included.
- Stop value is excluded.
- Default start is
0. - Default step is
1.
Examples
for i in range(5):
print(i)
for i in range(1, 6):
print(i)
for i in range(1, 11, 2):
print(i)Reverse and Empty Range
Use a negative step for reverse order.
for i in range(10, 0, -1):
print(i)If a valid sequence cannot be generated, the range is empty.
Common Uses
Use range() for counters, indexes, sums, and repeated sequences.
🧠 Test Your Knowledge
3 QuestionsProgress: 0 / 3
Keep Going!Python - Sets