Table of contents
1.
Introduction
2.
Python range() Function
2.1.
Syntax of Python range() Function
3.
Parameters
4.
Return
5.
What is the Use of the range() Function in Python?
6.
Python range(stop)
6.1.
Python range(start, stop)
6.2.
Python range(start, stop, step)
6.3.
Incrementing the Range using a Positive Step
6.4.
Python range() Using Negative Step
6.5.
Python range() with Float Values
7.
Python range() with More Examples
7.1.
Example 1: Looping through a range
7.2.
Example 2: Creating a list
8.
Some Important Points to Remember About the Python range() Function
9.
Frequently Asked Questions
9.1.
What happens if the step value is 0 in range()?
9.2.
Can range() generate a descending sequence?
9.3.
How to generate floating-point ranges in Python?
10.
Conclusion
Last Updated: Aug 21, 2025
Easy

Python range() Function

Author Rahul Singh
0 upvote

Introduction

Python is a widely used programming language valued for its clarity and ease of use. Among its many built-in features, the range function in Python stands out as a key tool for generating number sequences. It is commonly applied in loops, iterations, and algorithms to simplify repetitive tasks.

Python range() Function

This article will cover the range() function, its syntax, parameters, return values, and various use cases. 

Python range() Function

The range() function is commonly used to generate a sequence of numbers, often in loops. It is especially useful in for loops for iterating over a specific range of values.

Syntax of Python range() Function

range(start, stop, step)

 

  • start: The starting point of the sequence (inclusive).
     
  • stop: The end point of the sequence (exclusive).
     
  • step: The difference between each number in the sequence (default is 1).

Parameters

  1. start: Optional. The number to start the sequence from. The default is 0.
     
  2. stop: Required. The number at which the sequence ends (exclusive).
     
  3. step: Optional. The step size or increment between numbers. Default is 1.

Return

The range() function returns an immutable sequence of numbers. You can convert it to a list or iterate over it using a loop.

What is the Use of the range() Function in Python?

The range() function in Python is used to generate a sequence of numbers. It is particularly useful when you need to iterate over a series of numbers using a for loop. Instead of manually creating a list of numbers, you can use the range() function to generate the desired sequence automatically.

The range() function is commonly used in scenarios like:

1. Iterating over a specific number of times in a loop
 

2. Accessing elements of a sequence (e.g., a list or a string) by their index
 

3. Generating a sequence of numbers based on a starting point, endpoint, & step size
 

With the help of the range() function, you can write more concise & efficient code when dealing with sequences of numbers.

Python range(stop)

When only the stop value is provided, the sequence starts at 0 and increments by 1 until it reaches the stop value.

Example of Python range(stop)

# Generate numbers from 0 to 4
numbers = range(5)
print(list(numbers))
You can also try this code with Online Python Compiler
Run Code


Output:

[0, 1, 2, 3, 4]

Python range(start, stop)

The range() function in Python can be used with either one, two, or three arguments. When used with two arguments, it takes the form: range(start, stop).

  • The `start` argument specifies the starting number of the sequence (inclusive).
     
  • The `stop` argument specifies the ending number of the sequence (exclusive).

For example:

for num in range(1, 6):
    print(num)
You can also try this code with Online Python Compiler
Run Code


Output:

1
2
3
4
5


In this example, the range() function generates a sequence of numbers starting from 1 (inclusive) and ending at 6 (exclusive). The for loop iterates over each number in the sequence and prints it.

Note that the `stop` argument is exclusive, meaning that the generated sequence includes numbers up to, but not including, the `stop` value.

Python range(start, stop, step)

In addition to specifying the start & stop values, you can also provide a third argument to the range() function: the step size. The step size determines the increment between each number in the sequence. 

The syntax is :

 range(start, stop, step).
You can also try this code with Online Python Compiler
Run Code

 

  • The `start` argument specifies the starting number of the sequence (inclusive).
     
  • The `stop` argument specifies the ending number of the sequence (exclusive).
     
  • The `step` argument specifies the increment between each number in the sequence.


For example:

for num in range(1, 10, 2):
    print(num)
You can also try this code with Online Python Compiler
Run Code


Output:

1
3
5
7
9


In this example, the range() function generates a sequence of numbers starting from 1 (inclusive), ending at 10 (exclusive), with a step size of 2. The for loop iterates over each number in the sequence & prints it.

With the step argument, you can generate sequences with custom increments. For example, range(0, 10, 3) would generate the sequence: 0, 3, 6, 9.

Incrementing the Range using a Positive Step

A positive step value increments the range.

Example

# Incrementing by 3
numbers = range(0, 10, 3)
print(list(numbers))
You can also try this code with Online Python Compiler
Run Code


Output:

[0, 3, 6, 9]

Python range() Using Negative Step

A negative step value decrements the range.

Example

# Decrementing from 10 to 1
numbers = range(10, 0, -2)
print(list(numbers))
You can also try this code with Online Python Compiler
Run Code


Output:

[10, 8, 6, 4, 2]

Python range() with Float Values

The range() function does not support float values directly. However, you can use the numpy library or list comprehensions as a workaround.

Example

# Using numpy for float ranges
import numpy as np
numbers = np.arange(0.5, 5.5, 0.5)
print(list(numbers))
You can also try this code with Online Python Compiler
Run Code


Output:

[0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]

Python range() with More Examples

Example 1: Looping through a range

# Using range in a loop
for i in range(4):
    print(f"Number: {i}")
You can also try this code with Online Python Compiler
Run Code


Output:

Number: 0
Number: 1
Number: 2
Number: 3

Example 2: Creating a list

# Generating a list of even numbers
even_numbers = [x for x in range(0, 11, 2)]
print(even_numbers)
You can also try this code with Online Python Compiler
Run Code


Output:

[0, 2, 4, 6, 8, 10]

Some Important Points to Remember About the Python range() Function

1. The range() function returns a sequence of numbers, not a list. To convert the sequence to a list, you can use the list() function. For example:

   numbers = list(range(1, 6))
   print(numbers)  
You can also try this code with Online Python Compiler
Run Code


Output: 

[1, 2, 3, 4, 5]


2. If you omit the start argument, the range() function starts from 0 by default. For example, range(5) is equivalent to range(0, 5).
 

3. The range() function accepts integer arguments. If you provide float arguments, it will raise a TypeError. If you need to generate a sequence with float values, you can use techniques like list comprehension or the numpy library.
 

4. The step argument can be negative, allowing you to generate sequences in reverse order. For example, range(10, 0, -1) generates the sequence: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1.
 

5. The range() function is memory-efficient because it generates numbers on the fly, rather than storing the entire sequence in memory. This makes it suitable for generating large sequences without consuming a significant amount of memory.

Frequently Asked Questions

What happens if the step value is 0 in range()?

The range() function raises a ValueError because a step of 0 results in an infinite loop.

Can range() generate a descending sequence?

Yes, by using a negative step value, you can generate a descending sequence.

How to generate floating-point ranges in Python?

You can use libraries like numpy or list comprehensions with custom logic for floating-point sequences.

Conclusion

In conclusion, the range in Python is a powerful feature for generating number sequences. Mastering its syntax and use cases helps in writing clean and efficient code.

Recommended Readings:

 

 

 

 

Live masterclass