🔁 Python range() — Comprehensive Guide + Practice Editor

Learn how range() really works (start/stop/step, negatives, slicing, indexing, membership tests, performance) and run code instantly using Pyodide.

Shortcut: Ctrl + Enter to Run.

What it is
An iterable of ints (lazy)
Common use
Loop counts / indices
Memory
Doesn’t store all numbers
Stop rule
Stop is excluded

1) What is range()?

range() creates a special object called a range object. It represents an arithmetic progression of integers:

Think of it as: “a compact recipe for numbers”, not a stored list.

Quick demo: range is NOT a list
r = range(5)
print(r)                 # range(0, 5)
print(type(r))           # <class 'range'>
print(list(r))           # [0, 1, 2, 3, 4]

2) Syntax (three forms)

A range(stop)

Starts at 0, increases by 1, stops before stop.

range(stop)
print(list(range(5)))    # 0..4

B range(start, stop)

Starts at start, increases by 1, stops before stop.

range(start, stop)
print(list(range(2, 7))) # 2..6

C range(start, stop, step)

Starts at start, adds step each time, and stops before stop.

range(start, stop, step)
print(list(range(1, 10, 2)))  # odds: 1,3,5,7,9

Golden rules (must remember)

  • Stop is excluded: range(5) ends at 4.
  • Step cannot be 0: range(1, 10, 0) is an error.
  • If step is positive, numbers go up. If step is negative, numbers go down.
  • If your step direction doesn’t “move toward” stop, you get an empty range.
Empty range examples
print(list(range(5, 1)))      # [] (step defaults to +1, but start>stop)
print(list(range(1, 5, -1)))  # [] (step is -1, but start<stop)

3) Understanding stop-excluded with real examples

“Stop excluded” is extremely useful because it makes ranges fit naturally with:

Counting and indexing examples
items = ["a", "b", "c", "d"]

# 1) exactly len(items) indices
for i in range(len(items)):
    print(i, items[i])

# 2) last index is len(items) - 1
print("Last item:", items[len(items) - 1])

4) Negative steps (reverse counting)

Use a negative step to count down. A simple pattern is: range(start, stop, -1) where stop is usually 0 or -1 depending on inclusion.

Countdown patterns
# 10 down to 1
print(list(range(10, 0, -1)))

# 10 down to 0 (include 0)
print(list(range(10, -1, -1)))

5) How many numbers will a range produce?

You can get the size using len(range(...)). This is fast because range is compact.

len(range(...))
print(len(range(10)))          # 10 numbers: 0..9
print(len(range(2, 10)))       # 8 numbers: 2..9
print(len(range(1, 10, 2)))    # 5 numbers: 1,3,5,7,9
print(len(range(10, 0, -1)))   # 10 numbers: 10..1

6) range as a “sequence” (indexing, slicing, membership)

A range acts like an immutable sequence:

  • r[0] gives the first value
  • r[-1] gives the last value
  • r[2:10:2] returns another range
  • x in r checks membership efficiently
Indexing + slicing + membership
r = range(3, 20, 3)  # 3,6,9,12,15,18

print(r[0])          # 3
print(r[-1])         # 18
print(list(r[1:4]))  # [6, 9, 12]

print(12 in r)       # True
print(14 in r)       # False

Common beginner mistakes

  • Forgetting stop is excluded: range(1, 5) gives 1,2,3,4 (not 5).
  • Using the wrong stop when reversing: to include 0 you need stop = -1.
  • Step direction mismatch: range(1, 5, -1) becomes empty.
  • Step = 0 is invalid: range(1, 10, 0) raises an error.

7) Practical recipes (copy-ready)

✅ Recipe: 1 to n (inclusive)
1..n inclusive
n = 10
for i in range(1, n + 1):
    print(i)
✅ Recipe: Even numbers up to n
Evens up to n
n = 20
for i in range(2, n + 1, 2):
    print(i)
✅ Recipe: Reverse loop over indices
Reverse indices
items = ["x", "y", "z", "w"]

for i in range(len(items) - 1, -1, -1):
    print(i, items[i])
✅ Recipe: Multiplication table (nested loops)
Table 1..5
for i in range(1, 6):
    for j in range(1, 6):
        print(i * j, end="\t")
    print()

8) Try it yourself — Pyodide Python Editor

This editor runs Python directly in your browser using Pyodide (Python compiled to WebAssembly). No installation required.

Python code
Loading Pyodide…

Tip: Use print() to see output. Use input() carefully (see note).

Output stdout / errors

About input() in browser

In this page, input() is redirected to a simple prompt popup.

9) Mini practice tasks (range-focused)

  1. Print numbers from 100 to 1 (reverse) using range.
  2. Print multiples of 7 from 7 to 70.
  3. Print squares of numbers from 1 to 10.
  4. Take n and print all odd numbers up to n.
  5. Print the index + character of a string using range(len(s)).