Introduction to Python Range Function
Range function in Python is used to generate a sequence of numbers over time or between given values. Range functions in Python 2 and Python 3 are different in a mechanism where the Range function in Python 2 is not so efficient to handle large numbers. As it returns a list of numbers and in Python 3, it returns an iterable, which is an object which is more memory efficient but takes more computation, but the parameters remain the same in both the versions, i.e., a start value, a stop value and a step value which is optional.
a = range(start, stop, step)
How Range Function Works?
Range function generates outputs immutable sequence of numbers, generally used in the case of loops. Arguments that we are passing to range constructor must be of integer type. It can be built in an ‘int’ or object that implements the __index__ special method. Range function performs all kinds of sequence operations except concatenation and repetition. Its because range follows a specific pattern or repetition, and concatenation violates this rule.
If we don’t pass the step method, it will take 1 as default for increment values, and if we don’t pass the start argument, then it will 0 as a default value. Arguments that are passed the index values, not actual values. Range functions allow us to implement collections.abc. Sequence and provide us with many features like element index lookup, slicing, and support negative indices.
a = list(range(0,10))
In the above program, we have created a range inside the list function so that we can print variable ‘a’ without a loop. We have used 0 as a start, 10 as an end index and nothing in the step argument. As we can see from the output list, starting from 0 and ending at 9. Value is index position 10 only. So the end value ends – 1. We have not passed the step value, so the range function has taken 1 as an increment by default. Step argument cannot take 0 or any floating-point value.
a = list(range(0,-10,-1))
In this case, also, you can see that we have a specified negative range, and the step is -1. It will run till -9 only. In this case, if our end value is negative, then we have to specify the step value; otherwise, the range function won’t work, and it will return the empty list. It is always recommended to make use of the range function instead of using a regular list or tuple because range takes the same amount of memory irrespective of range size.
1. range(5) or range(end): In the above code, we have only specified a single value, so the value will be treated as an end, and the start value will be 0 by default, and the step will be 1 by default.
0,1,2,…,end - 1 => 0,1,2,3,4
2. range(1,5) or range(start,end): In this, we have specified the start and end parameter, and we didn’t specify the step, so it will be 1 by default.
start, start+1, start+2,…….,end-1 => 1,2,3,4
3. range(1,10,2) or range(start, end, step): In this, we have specified all the arguments to execute according to the arguments.
Examples to Implement Range Function
Here are some of the examples to implement python range function as follows:
Example #1 – Range Function Inside the Loop
Loop in the function used to run the code sequence repeatedly until all the list values are traversed. Loop works on the list, and the range function gives the output as a list, so we can directly use the range function inside the loop.
for i in range(0,21,2):
print(i, end=', ')
In the above program, you can see that we used for loop and passed a range function that is running from 0 to 21, and the step is 2.
Example #2 – Reversing Range Function
We can use the range to loop through negative values. Let’s look at an example.
for i in range(0,-21,-2):
print(i, end=', ')
In the above program we have used 0 as a start, end value as -21. So range function will only iterate till value 20 and step is -2. So it will be like 0, 0 (+) -2, 0 + (2)*-2, 0 + (2)*-3 …
Example #3 – Using Floating Values in Range Function
Let’s try to run the range function using floating numbers as arguments.
As you can see, if we defined the floating value inside the range, i.e. 10.5 and python has returned as float object error as python was expecting an integer value and it has failed to interpret floating value into an integer value.
Example #4 – Finding Sum and Average of n Numbers
This is the example of finding the average of n numbers; let’s see the code.
a = int(input('Enter Number: '))
sum = 0
avg = 0
for i in range(a):
avg = sum/a
In the above program, we have used the user input function to enter his desired value and then have initialized two variables with zero that to store the result. Then we have iterated the loop using the range function, passed the user input value, and added every value into the sum variable until the loop finished sum will have a total of the values. Now divide the sum with user input to find the average. It’s very easy with range to do any calculation or operation.
The range is a very useful function; it takes 3 values as arguments, starts end, and step. The step must not be zero; otherwise, it will generate python error, and arguments should be integer values, floating-point numbers won’t work. Range function output as a list and can be directly used inside the loop.
This is a guide to Python Range Function. Here we discuss the introduction and working of Python Range Function along with its example and explanation. You can also go through our suggested articles to learn more –