# All You Need To Know About Python Range Inclusive

A day without a for loop is incomplete for a programmer. It’s quite fascinating how a single syntax creates a much easier functionality to loop through the series of objects. Generally, we loop from 0 (which is considered to be the starting index) to the length of a certain array/list. But often or not, we are not aware whether the python range() function which we use are inclusive or not.

Python’s range function allows us to create a sequence of numbers starting from the start and stop integer. It starts from 0 and increments each value by one by default. The function stops at the specified stop number. In this article, we will be discussing how the Python range function is inclusive.

Contents

## Function Syntax and Returns

`range(start, stop, step)`

start – Starting integer of the sequence. Set to 0 by default
stop – Final integer that the sequence stops at. The number itself is not included.
step – Integer specifying incrementation. Set to 1 by default

### Returns

Returns integer values starting from lower-bound to the number before the upper-bound

## Is Python Range Inclusive or Exclusive?

The Python range function is exclusive. It beings with the start argument and does not finish with the stop argument of the function. An inclusive Python range would make sure that the stop integer is included as well.

## Why Does Python Range DOES NOT Include Upper Bound?

In Python programming, 0-based indexing is followed. This means, that 0 is considered a value. The function range(0,10), will result in numbers from 0 to 9. The stop is not included due to the exclusivity of the range function.

## How To Create an Iterator Which Includes The Lower and Upper Bounds?

The following program shows how we can implement our own range function that includes both lower and upper bounds.

```for x in range(1,math.floor(math.sqrt(x))+1):
y = math.sqrt(n - x * x)
```

Another solution is to call the range like this: `range(math.floor(math.sqrt(x))`

We can add +1 within the for-loop. However, it is recommended you use the previous solution for more efficiency.

## Can The Python Range be Backward Inclusive?

Python range can be made backward inclusive. With the help of the `reversed() `function, this is achieved. We can wrap the `reversed()` function around `range() `which returns a sequence in reverse inclusivity.

```for num in reversed(range(10)):
print(num)
```

Output

```9
8
7
6
5
4
3
2
1
0```

## How to Increase the Upper Bound of The Range Function

The Python range function has limitations to the upper-bound value. In order to generate a long sequence of numbers, we must use the `xrange` function. It comparatively uses less memory than Python `range`. Let’s take a look at the following demonstration.

```for i in range(1,5356356357331354553):
# This method will give rise to the OverflowError

for i in xrange(1, 5356356357331354553):
print i
```

## FAQs on Python Range Inclusive

Is xrange inclusive?

No. Python xrange is exclusive. It does not include the upper-bound value in the results.

Is Range Immutable?

The sequence of numbers returned by the range function is considered to be immutable.

## Conclusion

We have discussed Python’s range function and its upper-bound exclusivity. We have also discussed an alternative function called `xrange()` and how it differs from the standard Python range function.

Subscribe
Notify of 