Python range() Function Tutorial with Examples
We will use Python range() function in this tutorial, range() function returns the integer numbers. In between from the start and stop defined numerical values in the range() method.
Python range() Function Syntax
range(start, stop, step)
Property | Details |
---|---|
start | It accepts numerical value which refers to the starting position and by default it starts from 0. |
stop | It accepts numerical value, it refers to the ending position and it’s an optional. |
step | It’s an optional argument and it takes numerical value. It refers to the difference between every step. If not mentioned, by default it increments by 1. |
Python range() Function Examples
Let us check out the range() method examples below. We are printing 7 numbers using range function, It start calculation from 0th index. We’ve passed the single argument stop in below range() function, and it takes start = 0 and step = 1 by default.
for i in range(7):
print(i, end=', ')
Following will be the output:
# Result:
0, 1, 2, 3, 4, 5, 6
Python range() Function Example with Start and Stop Arguments
In the below example, we have passed two arguments start and stop, using for loop we are getting a range between 2 defined numbers. We haven’t mentioned step argument but as I told before it takes 1 step by default.
for i in range(4, 12):
print(i, end=', ')
See the output below:
# Result:
4, 5, 6, 7, 8, 9, 10, 11,
The range() Method Example in Python with Start, Stop and Step Arguments
In this example, we are about to use start, stop, and step arguments to get the even numbers.
for i in range(2, 10, 2):
print(i, end=', ')
Check out the output:
# Result:
2, 4, 6, 8,
Python range() Method Example with For Loop
When it comes to iterating over a data list, there is no better option then for loop. We can use range() function with for loop to iterate over a data collection.
Let us understand it by a small example, let’s say if we have 6 numbers and we want to add 3 every time a loop runs. We can easily calculate this using range() with for loop in Python.
numArray = [2, 5, 14, 16, 35]
for i in range(len(numArray)):
print("Original val ", numArray[i], "=>", "Converted val ", numArray[i] + 3)
See the output below:
Original value | Converted value |
---|---|
2 | 5 |
5 | 8 |
14 | 17 |
16 | 19 |
35 | 38 |
Analysing range() vs xrange() Functions in Python
If you’ve worked on Python 2 then you must be aware of xrange(), there is a version difference between range() and xrange() in Python. The xrange() function was renamed to range() in Python 3. However range() method works differently in Python 2 and Python 3.
Now the main question is why xrange() function was deprecated in Python 3? Well, it was done to enhance the program speed and save memory. Check out the differences below.
Python 2 xrange() Example
a = type(xrange(12))
print(a)
# output: class 'xrange'
Python 2 range() Example
a = type(range(12))
print(a)
#Output: <class 'range'>
Python 3 range() Example
a = type(range(12))
print(a)
# output: <class 'range'>
Workflow xrange() and range() in Python 2
- Let us understand the workflow of both range and xrange in Python 2, range and xrange() methods create the sequence of numerical values. Whereas range creates a list, and xrange generates an object.
- The xrange() function in Python generates numerical values one by one like a for loop iterates.This function doesn’t support creating all the number at once.
- All the integer numerical values are created at once by range function.
Python range() Function with Floats Type Example
By default float, types are not supported by range() function in Python. However, we can use a custom function to make the float works with range method.
def customFloatRange(start, stop=None, step=None):
if stop == None:
stop = start + 0.0
start = 0.0
if step == None:
step = 1.0
while True:
if step > 0 and start >= stop:
break
elif step < 0 and start <= stop:
break
yield ("%g" % start)
start = start + step
print ("Float num range output:")
customFloatList = customFloatRange(0.2, 1.0, 0.2)
for customFloatNum in customFloatList:
print (customFloatNum)
Check out the output:
# Result
# 0.2
# 0.4
# 0.6
# 0.8
Python Reverse range Examples
To get the numerical values in reverse order in Python, we have 2 most convenient methods.
First Method
Set the step argument to -1. Let’s say if you want to display numerical values in descending order. You can pass the negative value in the third argument.
for num in range(4,-1,-1):
print (num, end=', ')
# Result: 4, 3, 2, 1, 0,
Second Method
Another method is pretty straightforward, we can use the reversed function to reverse a data list. The critical point is to be noted here is we need to convert the range result to list first. Let’s see how it is done; check out the example below.
print ("Reversed range output:")
reverse_range = list(reversed(range(0,6)))
print(reverse_range)
# [5, 4, 3, 2, 1, 0]