Last modified: Feb 02, 2026 By Alexander Williams

Python Range Function Guide: Syntax & Examples

The range() function is a cornerstone of Python programming. It generates sequences of numbers. This makes it essential for loops and list creation.

Understanding range() is a key step for any beginner. It provides a powerful and memory-efficient way to handle repetitive tasks.

What is the Range Function in Python?

The range() function returns an immutable sequence of numbers. It is not a list but a special range object. This object generates numbers on the fly.

This design is memory efficient. It doesn't store all numbers in memory at once. It calculates them as needed during iteration.

It is most commonly used with for loops. It controls how many times a loop runs. It's also used to create lists of numbers.

Range Function Syntax and Parameters

The range() function can be called with one, two, or three arguments. Its full syntax is range(start, stop, step).

Only the stop argument is required. The start defaults to 0. The step defaults to 1.

Here is the breakdown of each parameter:

  • start: The first number in the sequence (inclusive). Default is 0.
  • stop: The sequence stops before reaching this number (exclusive). This is required.
  • step: The difference between each number. Default is 1. It can be negative.

For a deeper dive into how Python functions are structured, see our Python Function Syntax Guide for Beginners.

Using Range with One Argument (Stop)

When you use one argument, it is the stop value. The sequence starts at 0 and increments by 1.


# Generate numbers from 0 up to (but not including) 5
for i in range(5):
    print(i)
    

0
1
2
3
4
    

The loop printed five numbers. It started at 0 and stopped before 5.

Using Range with Two Arguments (Start, Stop)

With two arguments, you define the start and stop. The step remains 1.


# Generate numbers from 2 to 7
for i in range(2, 8):
    print(i)
    

2
3
4
5
6
7
    

This is useful when you don't want to start from zero.

Using Range with Three Arguments (Start, Stop, Step)

Three arguments give you full control. You set the start, stop, and step value.


# Count from 0 to 10 by 2s (even numbers)
for i in range(0, 11, 2):
    print(i)
    

0
2
4
6
8
10
    

You can also use a negative step to count backwards.


# Count down from 5 to 1
for i in range(5, 0, -1):
    print(i)
    

5
4
3
2
1
    

Common Use Cases and Practical Examples

range() is versatile. Here are some practical ways to use it.

1. Creating Lists and Tuples

You can convert a range object to a list or tuple.


# Create a list of numbers
my_list = list(range(5))
print("List:", my_list)

# Create a tuple of numbers
my_tuple = tuple(range(2, 6))
print("Tuple:", my_tuple)
    

List: [0, 1, 2, 3, 4]
Tuple: (2, 3, 4, 5)
    

2. Iterating Over List Indices

Use range() with len() to loop by index.


fruits = ['apple', 'banana', 'cherry']
for i in range(len(fruits)):
    print(f"Index {i}: {fruits[i]}")
    

Index 0: apple
Index 1: banana
Index 2: cherry
    

3. Generating Number Sequences for Calculations

It's great for mathematical sequences or simulations.


# Sum of numbers from 1 to 100
total = sum(range(1, 101))
print("Sum 1-100:", total)
    

Sum 1-100: 5050
    

Important Behaviors and Tips

Remember these key points about range().

The sequence is exclusive of the stop value. The last number is stop - 1.

The range() object is not a list. You must convert it to see all numbers at once.


r = range(3)
print(r)        # This prints the range object itself
print(list(r))  # This converts it to a list
    

range(0, 3)
[0, 1, 2]
    

You can use negative steps. But if start is less than stop with a negative step, the range is empty.


# This will not produce any output
for i in range(1, 5, -1):
    print(i)  # Nothing happens
print("Loop done.")
    

Loop done.
    

Understanding how arguments are passed and unpacked can further your skills. Learn more in our guide on Python Function Argument Unpacking.

Conclusion

The Python range() function is a fundamental tool. It creates number sequences efficiently.

You learned its three forms: range(stop), range(start, stop), and range(start, stop, step). Each serves a different purpose.