Python The range() Function
Need to repeat an action 10 times? You do not need to create a list of 10 numbers by hand. The built-in range() function generates a sequence of numbers on demand with start, stop, and step controls.
"range() is like an automatic ticket dispenser at a deli counter. It prints ticket #1, #2, #3, etc. on demand without wasting paper by printing 1,000 tickets up front."
Deep Dive: How It Works
range(stop): Generates numbers from 0 up to (but not including) stop. range(5) produces 0, 1, 2, 3, 4.
range(start, stop): Generates numbers from start up to (but not including) stop. range(1, 6) produces 1, 2, 3, 4, 5.
range(start, stop, step): Steps by a custom increment. range(0, 10, 2) produces even numbers 0, 2, 4, 6, 8. You can also count backward with negative steps: range(5, 0, -1).
Memory Efficiency: range() is a generator object that produces numbers as requested rather than allocating a huge array in RAM.
Syntax Blueprint
range(5) # 0, 1, 2, 3, 4 range(1, 6) # 1, 2, 3, 4, 5 range(0, 10, 2) # 0, 2, 4, 6, 8 (step by 2)
Start is inclusive, Stop is exclusive, and Step is the stride amount.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Expecting range(1, 5) to include 5Why it happens: In programming, upper bounds in ranges and slices are exclusive (up to, but not including).
How to fix: If you need numbers 1 through 5 inclusive, write range(1, 6).
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Print numbers 1 to 4 with range
Use a for loop with range(1, 5) to print numbers 1, 2, 3, and 4.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.