GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python range() function - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python range() function

Last Updated : 10 Mar, 2026

The range() function in Python is used to generate a sequence of integers within a specified range. It is most commonly used in loops to control how many times a block of code runs.

Note: range() returns a lazy iterable, not a full list. It generates numbers dynamically instead of storing them all in memory.. To access elements like a list, convert it using list(range(...)).

Example: This example shows the use of range() to generate numbers starting from 0 up to (but not including) a given value.

Python
for i in range(5):
    print(i, end=" ")

Output
0 1 2 3 4 

Explanation:

  • range(5) generates numbers from 0 to 4
  • i takes one value at a time from the generated sequence

Syntax

range(start, stop, step)

Parameters:

  • start (optional): Starting number of the sequence (default is 0)
  • stop: Number at which the sequence stops (not included)
  • step (optional): Difference between consecutive numbers (default is 1)

Return: A range object representing the sequence

Examples

Example 1: This example generates numbers starting from a custom value and ending before another value.

Python
for n in range(5, 10):
    print(n, end=" ")

Output
5 6 7 8 9 

Explanation:

  • range(5, 10) starts at 5 and stops before 10
  • Numbers increase by the default step of 1

Example 2: This example generates even numbers by skipping values using a custom step size.

Python
for v in range(0, 10, 2):
    print(v, end=" ")

Output
0 2 4 6 8 

Explanation:

  • range(0, 10, 2) increases the value by 2 each time
  • The step controls how much the number changes per iteration

Example 3: This example demonstrates how range() can be used to generate numbers in reverse order by providing a negative step value.

Python
for i in range(10, 0, -2):
    print(i, end=" ")

Output
10 8 6 4 2 

Explanation:

  • range(10, 0, -2) starts from 10 and decreases by 2 each time
  • A negative step allows backward iteration through numbers
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.