Python range function

The Python range function is one of the Built-in function, which helps to generate sequence of numbers. Or the Python range function helps to iterate items in an iterables such as Lists, Tuples, Sets, Strings, etc.

In this section, we discuss how to use this Python range function to generate numbers from a start point to end point with multiple examples.

Python range syntax

The syntax of the Python range function

range(start, end, step)

All the arguments in the Python range function accepts integer values. Either it can be positive or Negative integers.

  • Start(optional) – Starting number of the range. If you omit this, Python range function will start from 0. 
  • Stop – A value before this number will be the range end value. For example, range(1, 10) print values from 1 to 9.
  • Step (optional) – Sequence of numbers generated by Python range function. It determines the space or difference between each integer values. For example, range(1, 10, 2) returns 1, 3, 5, 7, 9. You can notice that the difference between each integer is 2.

Python range examples

The following list of Python range function examples generate a sequence of numbers from the given starting point to end point using the specified steps. To display the range of items are more number of items in a single statement can be possible by assigning the output to a iterable.

Python range in list example

We use a single argument in this list range example, which will be the stop value. The first statement starts at 0 and ends at 0. The second one starts at 0 and stops at 5. Similarly, the last statement starts at 0 and ends at 9. Here, we are using the list function to bind and display the numbers generated by the range function.

TIP: Refer the Python List article in Python.

# Python range function example
print(list(range(0)))
 
print(list(range(5)))
 
print(list(range(10)))

range list output

[]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Python Tuple range function

In this example also, we used a single argument. However, this time we are using the tuple function to display the items generated by the range function. Refer the Python Tuple article

print(tuple(range(0)))
 
print(tuple(range(5)))
 
print(tuple(range(10)))
()
(0, 1, 2, 3, 4)
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Python range function with two arguments

This examples uses range function with two arguments, and they are starting point and end point. For example, print(list(range(5, 15))) print list of values from 5 to 14 and print(list(range(150, 165))) returns list of values from 150 to 164.

print(list(range(0, 5)))
 
print(list(range(5, 15)))
 
print(list(range(10, 15)))
 
print(list(range(25, 33)))
 
print(list(range(150, 165)))
[0, 1, 2, 3, 4]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[10, 11, 12, 13, 14]
[25, 26, 27, 28, 29, 30, 31, 32]
[150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164]

In Python range function, you can use the negative values as the argument values. Here, we used negative values as the starting point, ending point. The first statement starts at -10 and ends at 0, and the second one starts at -15 and ends at -4.

print(list(range(-10, 1)))
 
print(list(range(-15, -5)))
 
print(list(range(-19, -4)))
 
print(list(range(-5, 5)))
 
print(list(range(-122, -110)))
 
print(list(range(0, -5)))
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0]
[-15, -14, -13, -12, -11, -10, -9, -8, -7, -6]
[-19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5]
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
[-122, -121, -120, -119, -118, -117, -116, -115, -114, -113, -112, -111]
[]
>>> 

Python range with step

Using Python range function with three arguments, and they are starting point, ending point and steps. Here, print(list(range(0, 10, 2))) print list of numbers from 0 to 9 and the interval difference between consecutive numbers should be 2. The last statement, print(list(range(150, 500, 50))) return numbers from 150 to 499 with interval 50.

print(list(range(0, 10, 2)))
 
print(list(range(5, 25, 5)))
 
print(list(range(2, 40, 4)))
 
print(list(range(25, 100, 5)))
 
print(list(range(150, 500, 25)))
 
print(list(range(150, 500, 50)))

range function with step output

[0, 2, 4, 6, 8]
[5, 10, 15, 20]
[2, 6, 10, 14, 18, 22, 26, 30, 34, 38]
[25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
[150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475]
[150, 200, 250, 300, 350, 400, 450]

Python range reverse

This is an example of Python range reserve. It means, you can use the range function to print items in reverse order also.

Here, we are using the negative values as the start point, end point and steps. If you use the negative values as the step argument, the range function returns values in reverse order. For example, print(list(range(300, 1, -25))) returns numbers from 300 to 1 with an interval difference of 25 (300, 275, 250….).

print(list(range(-10, 1, 2)))
 
print(list(range(-45, -5, 5)))
 
print(list(range(300, 1, -25)))
 
print(list(range(-5, 15, 3)))
 
print(list(range(450, -200, -50)))
 
print(list(range(1000, 1, -75)))

Python range reverse function output

[-10, -8, -6, -4, -2, 0]
[-45, -40, -35, -30, -25, -20, -15, -10]
[300, 275, 250, 225, 200, 175, 150, 125, 100, 75, 50, 25]
[-5, -2, 1, 4, 7, 10, 13]
[450, 400, 350, 300, 250, 200, 150, 100, 50, 0, -50, -100, -150]
[1000, 925, 850, 775, 700, 625, 550, 475, 400, 325, 250, 175, 100, 25]
>>>

Python for loop range function

Until now, we are using the lists or tuples to hold the range items, and print that list as an output. Generally, we use this range function along with For Loop to iterate any iterables.

The first for loop prints values from 0 to 4, and the next for loop generate values form 0 to 9. However, within the second for loop, we are multiplying each number by 2. So, the output will be 0, 2, 4, 6 etc.

for i in range(5):
    print(i)
 
print('-----------')
for i in range(10):
    print(i * 2)
0
1
2
3
4
-----------
0
2
4
6
8
10
12
14
16
18
>>>

Using Python for loop range function with two arguments. To keep the output short, we used the end argument inside the print statement.

for i in range(10, 20):
    print(i, end = ', ')
 
print('\n')
for i in range(150, 160):
    print(i, end = ', ')
 
print('\n')
for i in range(1, 16):
    print(i, end = ', ')
Python Range Function 8

Python for loop range function with steps. The first for loop starts at 12 and ends at 119. Here the interval difference between two numbers is 10. 

for i in range(15, 110, 10):
    print(i, end = ', ')
 
print('\n')
for i in range(150, 1, -10):
    print(i, end = ', ')
 
print('\n')
for i in range(-150, -300, -25):
    print(i, end = ', ')
 
print('\n')
for i in range(-300, -150, 25):
    print(i, end = ', ')
 
print('\n')
for i in range(-100, 500, 50):
    print(i, end = ', ')
 
print('\n')
for i in range(400, -200, -50):
    print(i, end = ', ')

for loop range function with step output

15, 25, 35, 45, 55, 65, 75, 85, 95, 105, 

150, 140, 130, 120, 110, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 

-150, -175, -200, -225, -250, -275, 

-300, -275, -250, -225, -200, -175, 

-100, -50, 0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 

400, 350, 300, 250, 200, 150, 100, 50, 0, -50, -100, -150, 

Python range string example

Use the range function to print the same statement or same string multiple times. The second statement prints Python from 1 to 4 (four times) 

for i in range(5):
    print('Python')
 
print('-----------')
for i in range(1, 5):
    print('Python')
 
print('-----------')    
for i in range(2**3):
    print('Tutorial Gateway')
Python
Python
Python
Python
Python
-----------
Python
Python
Python
Python
-----------
Tutorial Gateway
Tutorial Gateway
Tutorial Gateway
Tutorial Gateway
Tutorial Gateway
Tutorial Gateway
Tutorial Gateway
Tutorial Gateway

In this Python range string example, we declared a string. Next, we used for loop to iterate each character in a string using the range function.

Within the range function, we used len function to find the length of a string and used it as the end point of the range function. It means, range starts at 0 and ends at len(company). Within the print statement, we used the index values to print the characters.

company = 'Tutorial Gateway'
 
for i in range(len(company)):
    print(company[i], end = ', ')
 
print('\n-----------')
for i in range(0, len(company)):
    print(company[i])
T, u, t, o, r, i, a, l,  , G, a, t, e, w, a, y, 
-----------
T
u
t
o
r
i
a
l
 
G
a
t
e
w
a
y

range iterator example

Let me use this Python range function to iterate the iterables like list and tuple. The first for loop with range is looping over list of fruits and printing them. Next for loop with range function iterates the tuple and prints the tuple items.

Fruits = ['Apple', 'Orange', 'Grape', 'Banana']
for i in range(len(Fruits)):
    print(Fruits[i])
 
print('--------------')
x = (10, 20, 30, 40, 50, 60, 70, 80, 90)
for i in range(len(x)):
    print(x[i])
Apple
Orange
Grape
Banana
--------------
10
20
30
40
50
60
70
80
90

Another example of range iterator on string, lists, and tuples.

string = 'Tutorial Gateway'
 
for i in range(len(string)):
    print(string[i], end = ', ')
 
print('\n')
numbers_list = [10, 20, 30, 40, 50]
 
for i in range(len(numbers_list)):
    print(numbers_list[i], end = ', ')
 
print('\n')
fruits_tuple = ('apple', 'cherry', 'kiwi', 'mango')
 
for i in range(len(fruits_tuple)):
    print(fruits_tuple[i], end = ', ')

range iterator on string, lists, and tuples output

T, u, t, o, r, i, a, l,  , G, a, t, e, w, a, y, 

10, 20, 30, 40, 50, 

apple, cherry, kiwi, mango,