Last modified: Mar 28, 2026 By Alexander Williams

Inclusive Range Python: How to Include Stop Value

Python's range() function is a core tool. It generates sequences of numbers. But it has one key behavior. It excludes the stop value by default. This guide explains how to make it inclusive.

Understanding the Standard Range Function

The built-in range() function creates an immutable sequence. It is often used in for loops. Its syntax is range(start, stop, step).

The start and step arguments are optional. The stop argument is required. The sequence stops before reaching the stop value. This is called an exclusive upper bound.


# Example of exclusive range
for i in range(0, 5):
    print(i)
    

0
1
2
3
4
# Note: 5 is not included in the output.

Why Isn't Range Inclusive by Default?

This design comes from Python's philosophy. It aligns with zero-based indexing. It also prevents common off-by-one errors. The length of the sequence is simply stop - start.

For a deeper dive into its syntax, see our Python Range Function Guide: Syntax & Examples.

Simple Method: Add 1 to Stop

The easiest way to make a range inclusive is to add 1. Adjust the stop parameter by one. This includes your desired final number.


# Creating an inclusive range from 1 to 10
start = 1
stop_inclusive = 10
for num in range(start, stop_inclusive + 1):  # Add 1 to include 10
    print(num)
    

1
2
3
4
5
6
7
8
9
10
# Now 10 is included.

Creating a Reusable Inclusive Range Function

You can write a helper function. This makes your code cleaner and reusable. It encapsulates the logic of adding 1.


def inclusive_range(start, stop, step=1):
    """Generates a range that includes the stop value."""
    return range(start, stop + 1, step)

# Using the custom function
for i in inclusive_range(5, 15, 2):
    print(i)
    

5
7
9
11
13
15
# Includes the final stop value 15.

Using NumPy's arange for Inclusive Ranges

For numerical computing, use NumPy. Its numpy.arange() function also excludes the stop. But you can combine it with a small tolerance. This is useful for floating-point numbers.


import numpy as np

# numpy.arange is also exclusive
exclusive_arr = np.arange(0.0, 1.0, 0.2)
print("Exclusive:", exclusive_arr)

# To be inclusive, adjust the stop slightly
inclusive_stop = 1.0 + 0.2  # Add the step value
inclusive_arr = np.arange(0.0, inclusive_stop, 0.2)
print("Inclusive:", inclusive_arr)
    

Exclusive: [0.  0.2 0.4 0.6 0.8]
Inclusive: [0.  0.2 0.4 0.6 0.8 1. ]

Practical Examples and Common Use Cases

Inclusive ranges are common in real tasks. You might need to process items up to a certain index. Or generate a list of numbers for a user.


# Example 1: Iterating through list indices inclusively
my_list = ['A', 'B', 'C', 'D', 'E']
last_index_to_process = 3  # We want to include index 3 ('D')

for i in range(0, last_index_to_process + 1):
    print(f"Index {i}: {my_list[i]}")

# Example 2: Generating a list of inclusive years
start_year = 2020
end_year = 2025
years = list(range(start_year, end_year + 1))
print("Years:", years)
    

Index 0: A
Index 1: B
Index 2: C
Index 3: D
Years: [2020, 2021, 2022, 2023, 2024, 2025]

For more targeted solutions, check our article on Python Range Inclusive: How to Include the Stop Value.

Important Considerations and Pitfalls

Be careful with negative steps. The logic reverses. To include the stop value, you might need to subtract 1.

Also, remember that range() works with integers. For floats, NumPy is better. Always test your inclusive logic.


# Inclusive range with a negative step
start = 10
stop_inclusive = 5  # We want to go down to 5
step = -1

# Since we are decrementing, subtract 1 from stop
for i in range(start, stop_inclusive - 1, step):
    print(i)
    

10
9
8
7
6
5
# Includes the lower stop value 5.

Conclusion

Python's range() is exclusive by design. Making it inclusive is straightforward. The most common method is to add 1 to the stop argument.

You can create a helper function for cleaner code. For advanced numerical work, consider NumPy. Understanding this concept prevents bugs.

Mastering inclusive ranges makes your Python code more intuitive and correct. It is a fundamental skill for any developer.