You are currently viewing Python – range() Inclusive Values

We know that range() function returns the range of values exclusive by default. That means if you provide stop as 100, It will return values from 0 to 99 excluding 100.

Advertisements

If you need to return Inclusive values i.e 100 also, you need to provide a value that is greater than the actual value passed as the stop parameter.

Let’s see different scenarios of the range() function in this tutorial with Inclusive values.

By specifying start, stop, and step parameters, By excluding the start and step parameters and excluding step parameters.

1. Quick Examples of using range() for Inclusive values


# Example 1: Inclusive range with stop parameter
for i in range(10+1):
    print(i)

# Example 2: Inclusive range with start and stop parameters
for i in range(1,10+1):
    print(i)

# Example 3: Inclusive range with all parameters
for i in range(1,10+1,2):
    print(i)

# Example 4: Inclusive range with negative step
for i in range(20,10+1,-3):
    print(i)

2. range() with Inclusive values

range() will return the values using start and stop parameters. If you need the range of values inclusively, you need to pass the value greater than the actual stop value i.e greater than 1.

2.1 range() Syntax

We can use the for loop to utilize the range() function.


for iterator in range(start,stop+1,step):
    statements...

2.2 Examples

Example 1: Let’s return a range of values exclusively & inclusively by specifying only the stop parameter.


# range() with stop parameter exclusively
for i in range(10):
    print(i,end=" ")

print()

# range() with stop parameter inclusively
for i in range(10+1):
    print(i,end=" ")

# Output:
# 0 1 2 3 4 5 6 7 8 9 
# 0 1 2 3 4 5 6 7 8 9 10 

First, we specified the stop parameter as 10. So the range() will return the data from 0 to 9 exclusively.

Next, we specified the stop parameter as 10+1 = 11. So the range() will return the data from 0 to 10 inclusively.

Example 2: Let’s return a range of values exclusively & inclusively by specifying only start and stop parameters.


# range() with start & stop parameters exclusively
for i in range(2,10):
    print(i,end=" ")

print()

# range() with start & stop parameters inclusively
for i in range(2,10+1):
    print(i,end=" ")

# Output:
# 2 3 4 5 6 7 8 9 
# 2 3 4 5 6 7 8 9 10 

First, we specified the start parameter as 2 and the stop parameter as 10. So the range() will return the data from 2 to 9 exclusively.

Next, we specified the start parameter as 2 and the stop parameter as 10+1 = 11. So the range() will return the data from 2 to 10 inclusively.

Example 3: Let’s return a range of values exclusively & inclusively by specifying all three parameters.


# range() exclusively
for i in range(2,11,3):
    print(i,end=" ")

print()

# range() inclusively
for i in range(2,11+1,3):
    print(i,end=" ")

# Output:
# 2 5 8 
# 2 5 8 11  

We are returning a range of values exclusively and inclusively with the increment of 3.

Example 4: Let’s return a range of values exclusively & inclusively by specifying all three parameters with negative step value.


# range() exclusively
for i in range(20,11,-2):
    print(i,end=" ")

print()

# range() inclusively
for i in range(20,11+1,-2):
    print(i,end=" ")

# Output:
# 20 18 16 14 12 
# 20 18 16 14 

We are returning a range of values exclusively and inclusively with a decrement of 2.

3. Inclusive vs Exclusive

Inclusive RangeExclusive Range
It will include the last number in the range.It will exclude the last number in the range.
We need to add 1 to the stop value to get inclusive range of values.By default, range() is exclusive

4. Conclusion

We have seen the exact difference between the inclusive and exclusive range of values returned by the range() function in python. By default, it will return an exclusive range of values. If you want to return the inclusive range, you need to add 1 to the stop value.