Introduction to Python Range Function
Range function in Python is used to generate a sequence of numbers over time or between given values. Range function in Python 2 and Python 3 are different in mechanism where 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 same in both the versions i.e, an start value, stop value and an step value which is optional.
Syntax:
a = range(start, stop, step)
How Range Function Works?
Range function generates outputs immutable sequence of numbers, generally used in 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 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 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.
Example:
a = list(range(0,10))
print(a)
Output:
In the above program, we have created a range inside the list function, so that we can print variable ‘a’ without loop. We have used 0 as a start, 10 as end index and nothing in 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 step value so by default, range function has taken 1 as an increment. Step argument cannot take 0 or any floating-point value.
Example:
a = list(range(0,-10,-1))
print(a)
Output:
In this case, also, you can see that we have a specified negative range and 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, range function won’t work and it will return the empty list. It is always recommended to make use of 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 start and end parameter and we didn’t specify step so it will be 1 by default.
4.8 (7,843 ratings)
View Course
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, so it will execute according to the arguments.
start, start+step,start+2step,start+3step,......,end-1
1,3,5,7,9
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 code sequence again and again until all the values of the list are traversed. Loop works on the list, and range function gives the output as a list, so we can directly use range function inside the loop.
Code:
for i in range(0,21,2):
print(i, end=', ')
Output:
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.
Code:
for i in range(0,-21,-2):
print(i, end=', ')
Output:
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.
Code:
a=list(range(0,10.4,1))
Output:
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.
Code:
a = int(input('Enter Number: '))
sum = 0
avg = 0
for i in range(a):
sum+=i
avg = sum/a
print(sum)
print(avg)
Output:
In the above program, we have used user input function so that the user can enter his desired value and then we have initialized two variables with zero that to store the result. Then we have iterated the loop using range function and passed user input value and keep on adding every value into the sum variable, till 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.
Conclusion
The range is a very useful function, it takes 3 values as arguments, start 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.
Recommended Articles
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 –