Coding/Learn/python/range
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnThe range() Function
20 / 32 0%
Lesson 20 of 32
6 mins read

The range() Function

PreviousNext Lesson
In Plain English

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.

The Mental Model

"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.

PointCore Rules to Remember

Point star
Stop is Exclusive: range(1, 5) stops at 4, not 5. The upper boundary is never reached.
Point star
Step Control: The third parameter lets you count by 2s, 5s, 10s, or backwards.
Point star
Memory Friendly: range(1000000) takes virtually zero memory because numbers are generated on the fly.

Common Beginner Traps & How to Fix Them

Mistake:Expecting range(1, 5) to include 5

Why 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).

Pro Tip: Convert a range directly into a list using list(): numbers = list(range(1, 11)).

Live Interactive Example

Hit Run Code to see it live
Counting and Stepping with range()
Python 3.12
1
2
3
4
5
6
7
8
9
Output Console
Click "Run Code" to view the rendered output.
How it works: range(3) yielded 0, 1, 2. range(2, 9, 2) stepped by 2 starting at 2 up to 8.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Print numbers 1 to 4 with range

  • PointUse a for loop with range(1, 5) to print numbers 1, 2, 3, and 4.
1
2
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates