The Python range function is one of the Built-in function, which helps to generates sequence of number, or to iterate items in an iterables such as Lists, Tuples 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 range function in Python
range(start, end, step)
- Start(optional) – This is the starting number of the range. If you omit this value, 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 – This is an optional argument that decides the 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.
All the arguments in the Python range function accepts integer values. Either it can be positive or Negative integers.
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 List range function
We use a single argument, 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)))
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.
TIP: Refer the Python Tuple article.
print(tuple(range(0))) print(tuple(range(5))) print(tuple(range(10)))
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)))
In Python, you can use the negative values as the argument values of the range function. Here, we used negative values as the starting point, ending point, and both start and end points. 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)))
Python range function with three arguments
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)))
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 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)
Using 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 for loop range function with three arguments. 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 = ', ')
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')
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])
Python range iterator example
Let me use this 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])
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 = ', ')