Course Progress23% complete
🐍 Python Basics Topic 23 / 100
⏱ 7 min read

range() in Depth

Everything range() can do — generating sequences of numbers for loops, countdowns, steps, and patterns.

"range() is deceptively simple. It looks like it just counts numbers — but it powers the vast majority of all loops you'll ever write."

— ShurAI

What is range()?

range() generates a sequence of integers — numbers in a row — that you can loop over. It is memory-efficient: it doesn't create a million numbers in memory at once, it generates them one at a time as the loop needs them.

range(stop)
Counts from 0 up to (not including) stop. The most common form.
range(start, stop)
Counts from start up to (not including) stop.
range(start, stop, step)
Counts from start to stop, jumping by step each time. Step can be negative for countdowns.

Form 1 — range(stop)

python
# range(5) = 0, 1, 2, 3, 4
for i in range(5):
    print(i, end=" ")
# 0 1 2 3 4

# Repeat something 3 times (ignore the number)
for _ in range(3):
    print("Hello!")
# Hello!
# Hello!
# Hello!
The _ convention

When you just need to repeat N times but don't actually use the loop number, write for _ in range(N). The underscore _ is a Python convention meaning "I'm not using this variable".

Form 2 — range(start, stop)

python
# range(1, 6) = 1, 2, 3, 4, 5
for i in range(1, 6):
    print(i, end=" ")
# 1 2 3 4 5

# Quiz questions numbered 1 to 10
for q in range(1, 11):
    print(f"Question {q}: ...")

Form 3 — range(start, stop, step)

python
# Even numbers 2 to 10
for i in range(2, 11, 2):
    print(i, end=" ")
# 2 4 6 8 10

# Multiples of 5 from 0 to 50
for i in range(0, 51, 5):
    print(i, end=" ")
# 0 5 10 15 20 25 30 35 40 45 50

# Countdown from 10 to 1
for i in range(10, 0, -1):
    print(i, end=" ")
# 10 9 8 7 6 5 4 3 2 1
range() Produces Common use
range(5)0 1 2 3 4Repeat 5 times
range(1, 6)1 2 3 4 51-based numbering
range(0, 11, 2)0 2 4 6 8 10Even numbers
range(1, 11, 2)1 3 5 7 9Odd numbers
range(10, 0, -1)10 9 8 ... 1Countdown

Real Example — Multiplication Table Grid

python
# Print a 5×5 multiplication grid
print("   ", end="")
for i in range(1, 6):
    print(f"{i:4}", end="")
print()
print("-" * 23)

for row in range(1, 6):
    print(f"{row} |", end="")
    for col in range(1, 6):
        print(f"{row * col:4}", end="")
    print()
output
      1   2   3   4   5
-----------------------
1 |   1   2   3   4   5
2 |   2   4   6   8  10
3 |   3   6   9  12  15
4 |   4   8  12  16  20
5 |   5  10  15  20  25

Converting range to a List

python
# Wrap in list() to see all values at once
print(list(range(5)))          # [0, 1, 2, 3, 4]
print(list(range(1, 6)))       # [1, 2, 3, 4, 5]
print(list(range(0, 10, 3)))   # [0, 3, 6, 9]

"range() seems boring — just numbers. But it is the engine behind loops, timers, grids, and indexing. Use it well."

— ShurAI

🧠 Quiz — Question 1

What does list(range(3)) produce?

🧠 Quiz — Question 2

What does range(2, 8, 2) produce?

🧠 Quiz — Question 3

How do you count DOWN from 5 to 1 using range?

🧠 Quiz — Question 4

What does for _ in range(4) do compared to for i in range(4)?