Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Python
  3. Question
Python

How does the Python's range function work?

Asked by Kunde Wayne Nov 2, 2025 879 views 2 answers
Share

About this question

How does Python’s range() function actually work behind the scenes?

What does it return, and how does it generate sequences efficiently without storing every number in memory?

Your answer

2 Answers

James Harwood Latest answer

Answered on Dec 19, 2025

Python's range() returns an immutable sequence object (an iterable) rather than a list. It generates numbers on demand using lazy evaluation, calculating each value only when needed based on its stored start, stop, and step parameters. Because it only stores these three integers, it maintains a constant memory footprint E-ZPass in Indiana ($O(1)$) regardless of whether the range represents ten numbers or ten billion.

Was this helpful?

More Python discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Python Blogs

Guides, tips and career advice on Python from JanBask experts.