To create a range in Python, use the range[] function. The range[] method in Python creates an immutable sequence of numbers starting from the given start integer to the stop integer.
Python range is inclusive because it starts with the first argument of the range[] method, but it does not end with the second argument of the range[] method; it ends with the end – 1 index. The reason is zero-based indexing.
It is more of a standard to call range[0, 10] which returns [0,1,2,3,4,5,6,7,8,9] which contains 10 elements which equals len[range[0, 10]].
numbers = range[1, 10] for data in numbers: print[data]
Output
1 2 3 4 5 6 7 8 9
The range[] method does not include the end number because it generates numbers up to the end number but never includes the end number in its result.
To include the 10 number in your output, you need to get a range between 1 to 11.
numbers = range[1, 11] for data in numbers: print[data]
Output
1 2 3 4 5 6 7 8 9 10
Remember that the range[] method only works with the integers, So all parameters must be integers. You can not use floating-point numbers or any other data type as a start, stop, and step value.
Python range[] syntax
range[start, stop[, step]]
Arguments
The range[] method accepts three arguments. Out of the three arguments, two are optional. The start and step are optional arguments, and the stop is the mandatory argument.
Pass start and stop values to range[]
In this range[] example, we will assign start=0 and stop = 5.
numbers = range[0, 5] for data in numbers: print[data]
Output
0 1 2 3 4
Passing the step value to range[] function
The step argument specifies the increment. For example, range[0, 5, 2]. Here, step = 2.
numbers = range[0, 5, 2] for data in numbers: print[data]
Output
0 2 4
Just passing stop argument
Let’s pass only the mandatory argument, which is stop.
for i in range[10]: print[i, end=' ']
Output
0 1 2 3 4 5 6 7 8 9
Here, start = 0 and step = 1 as a default value. If you set the stop as a 0 or some negative value, then the range returns the empty sequence. If you want to start the range at 1, use range[1, 10].
That is it for Python range inclusive example.
Related posts
Python range reverse
np.arange
In this short tutorial, we look at the inclusive range Python function. We explain what inclusive signifies with the help of examples.
Table of Contents - Inclusive range Python
- Inclusive range Python
- Code and Explanation
- Closing thoughts - Inclusive range Python
Inclusive range Python
The Python range function is commonly used to create a series of numbers. These numbers can be printed or used in loops.
for values in range[0,4]:
print[values]
Although the range in the code block says 0-4. The output is as follows.
0
1
2
3
This is because Python prints values from 0 until 4. This behavior is called off-by-one and is common in programming languages. Essentially the function does not include the end value. This behavior is favorable while looping through objects since the index starts at 0. However, in case you are looking to print values or if you are new to Python, this can take some time to get used to.
While programming the best practice is to understand the language and use it to its strengths. Although I would recommend understanding and using the syntax as it is, there are a few methods to make range inclusive.
Code and Explanation:
Let us first take a look at the syntax of the range[] function.
Syntax:
range[start, stop, Increment]
Parameters:
- Start - Optional, used to specify where the range should start. The default start value is 0.
- Stop - Required, used to specify where the range should stop.
- Increment - Optional, used to specify the incrementation.
The right way to print values from 0 to 10 would be as follows:
for values in range[0,11]:
print[values]
This would print out values from 0 - 10.
Another method is to create a function which increments the range. This makes the Python range function inclusive. The code is as follows:
def range_inclusive[start, end]:
return range[start, end+1]
range_inclusive[1, 10]
When the function is called, it adds 1 to the stop parameter. The output is as follows.
0
1
2
3
4
5
6
7
8
9
10
Closing thoughts - Inclusive range Python
Although the above method works properly it is not advisable to use it. I would only recommend using it for learning purposes. Product development would involve working with multiple developers and using such syntax would not be advisable.