Python range function

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 a stop point with multiple steps 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.

range(start, stop, step)
  • Start(optional) – Starting position number. If you omit this, the Python range function will start from 0.
  • Stop – A value before this number is 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 value. For example, (1, 10, 2) returns 1, 3, 5, 7, and 9. You can notice that the difference between each integer is 2.

Python range examples

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

Python List range example

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]

Python Tuple range example

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.

TIP: Refer to the List and Tuple articles in Python.

(0, 1, 2, 3, 4)

Python range function with two arguments

This Python range example uses two arguments, and they are starting point and the stop argument. For example, print(5, 15) the print sequence of numbers starting from 5 to 14, and (150, 165) returns the sequence of Integers from 150 to 164 with the default value as the step.

You can also use the negative values as the range argument values. Here, we also used negative values as the starting and ending points. 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 start, stop, and step arguments

Using range object with three arguments, they are starting point, ending point, and steps. Here, (0, 10, 2) print a 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 an interval of 50. The result will be [150, 200, 250, 300, 350, 400, 450].

Python range function with negative values to reverse

This is an example of a range 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 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 iterable.

The for loop range generates and prints values from 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)

Using for loop range with two arguments. We used the end argument inside the print statement to keep the output short.

Python Range Function 8

Python for loop range function with steps argument

Here, we used the steps. The first for loop begins at 15 and ends at 110. Here the interval difference between the two numbers is 10.

for i in range(15, 110, 10):
    print(i, end = ', ')
15, 25, 35, 45, 55, 65, 75, 85, 95, 105,  

Python range function to print string text

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

Python range function to print characters in a string

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 the 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, 

Python range function iterator example

Let me use this Python range function to iterate the iterables like lists and tuples. The first one is looping over a list of fruits and printing them. The next one iterates the tuple and prints the tuple items.

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