Python range() Function Explained — Syntax & Easy Examples
Learn range() function in Python with 10 simple examples — from basic sequences to negative steps, lists, and reverse loops. Perfect for beginners preparing for interviews or practice.
10 Examples of Using the range() Function in Python
Generate 0 to 9
# Print the numbers 0 to 9
for i in range(10):
print(i)
Output
0 1 2 3 4 5 6 7 8 9
Generate 3 to 9
for i in range(3, 10):
print(i)
Output
3 4 5 6 7 8 9
Step Size = 2
for i in range(0, 10, 2):
print(i)
Output
0 2 4 6 8
-10 to -1
for i in range(-10, 0):
print(i)
Output
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Step Size = 3
for i in range(-10, 0, 3):
print(i)
Output
-10 -7 -4 -1
-5 to 5 (list)
sequence = list(range(-5, 5))
print(sequence)
Output
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
Create a List (0–9)
numbers = list(range(10))
print(numbers)
Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
List with Step = 2
numbers = list(range(0, 20, 2))
print(numbers)
Output
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
0.0 to 1.0 (step 0.1)
range()
works with integers; generate 0–10 and divide for clean decimals (avoids float precision issues).
for i in range(0, 11):
print(i / 10)
Output
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
Reverse: 10 → 1
for i in range(10, 0, -1):
print(i)
Output
10 9 8 7 6 5 4 3 2 1
These Python range() examples cover start, stop, and step usage, negative ranges, list conversion, and reverse iteration—matching common queries like “range function in python”, “python range examples”, “use of range function in python”.