The Python range function helps to generate a sequence of numbers. Or this Python range function helps to iterate items in 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 stop point with multiple step examples.
The syntax of this Python function contains range start, stop, step. All the arguments such as start, stop, and step accepts positive or Negative integers.
- Start(optional) – Starting position number. If you omit this, it will start from 0.
- Stop – A value before this number will be the end value. For example, (1, 10) print values from 1 to 9.
- Step (optional) – Sequence of numbers generated. It determines the space or difference between each integer values. For example, (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 stop point using the specified steps. To display the more number of items in a single statement, it can be possible by assigning the output to a iterable.
We use a single argument in this list object Python range function example, which will be the stop value. The first statement starts at 0 and stops at 0, which means it generates an empty sequence.
The second range object starts at 0 and stops at 5. Similarly, if we use 10 as the argument value (stop), this method starts at 0 and ends at 9. Here, we are using the list function to bind and display the numbers generated by it.
 [0, 1, 2, 3, 4]
In this Python range of integers example also, we used a single argument. However, this time we are using the tuple function with only one argument to display the items generated.
(0, 1, 2, 3, 4)
Python range function with two arguments
This examples uses two arguments, and they are starting point and stop argument. For example, print(5, 15) print sequence of numbers starting from 5 to 14 and (150, 165) returns sequence of Integers from 150 to 164 with default value as the step.
You can also use the negative values as the argument values. Here, we also used negative values as the starting point, ending point. The fourth statement starts at -10 and ends at 0, and the fifth one begins at -15 and stops at -4.
print(list(range(5, 15))) print(list(range(-10, 1)))
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14] [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0]
Python range step
Using Python range object with three arguments, and they are starting point, ending point and steps. Here, (0, 10, 2) print list of numbers from 0 to 9 and the interval difference between consecutive numbers should be 2.
print(list(range(0, 10, 2)))
[0, 2, 4, 6, 8]
If we use something like, (150, 500, 50) return numbers from 150 to 499 with interval 50. The result will be [150, 200, 250, 300, 350, 400, 450].
Python range reverse
This is an example of reserve. It means, you can use this to print items in reverse order also.
Here, we are using the negative values as the start point, stop point, and steps. If you use the negative values as the step argument, the reverse function returns values in reverse order. For example, (300, 1, -25) with. negative step returns numbers from 300 to 1 with an interval difference of 25 (300, 275, 250….).
print(list(range(-10, 1, 2))) print(list(range(300, 1, -25)))
[-10, -8, -6, -4, -2, 0] [300, 275, 250, 225, 200, 175, 150, 125, 100, 75, 50, 25]
Python for loop range function
Until now, we are using the lists or tuples to hold the elements, and return that list as an output. Generally, we use this function along with For Loop to iterate any iterables.
The for loop range generate and prints values form 0 to 9. However, within the print method, we are multiplying each number by 2. So, the output will be 0, 2, 4, 6 etc.
for i in range(10): print(i * 2)
0 2 4 6 8 10 12 14 16 18
Using Python for loop range with two arguments. To keep the output short, we used the end argument inside the print statement.
Here, we used the steps. The first for loop begins at 15 and ends at 110. Here the interval difference between two numbers is 10.
for i in range(15, 110, 10): print(i, end = ', ')
15, 25, 35, 45, 55, 65, 75, 85, 95, 105,
Use the Python range function to print the same statement or same string multiple times. The first statement prints from 1 to 4 (four times). By default, it begins at 0 and prints five times, however, we used one as the first argument so it prints four times. In the second one, we removed the remove the first argument. If you use (0, 2**3) as the argument, it will print the string eight times.
for i in range(1, 5): print('Tutorial Gateway')
Tutorial Gateway Tutorial Gateway Tutorial Gateway Tutorial Gateway
In this example, we declared a string. Next, we used for loop to iterate each character in a string using this method.
Within it, we used built-in len method to find the length of a string and used it as the end point of this function. It means, the loop begins at 0 and stops at len(company). We used the index values to print the characters.
company = 'Tutorial Gateway' for i in range(len(company)): print(company[i], end = ', ')
T, u, t, o, r, i, a, l, , G, a, t, e, w, a, y,
Let me use this Python range function to iterate the iterables like list and tuple. The first one is looping over list of fruits and printing them. Next one iterates the tuple and prints the tuple items.
Fruits = ['Apple', 'Kiwi', '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 Kiwi Grape Banana -------------- 10 20 30 40 50 60 70 80 90