Latest Code Tutorials

Python range: How to Use range() Function in Python

Often you will want to use the range() function when you want to perform an action let say x number of times, where you may or may not care about the index then this function is useful. Sometimes you may want to iterate over the list or iterable object while being able to have the index available.

Python Range Example

Python range() is an inbuilt function that is useful when you need to perform an operation a specific number of times. The range() method returns an immutable sequence of numbers between a given start integer to a stop integer.

It creates a sequence of numbers from start to stop integer. By the end of this guide, you will understand the basics of range() function in python.

The syntax for range() method is the following.

range(start, stop[, step])

The range() takes mainly three arguments.

  1. start – integer starting from which the sequence of integers is to be returned
  2. stop – integer before which the sequence of integers is to be returned.
    The range of integers ends at stop – 1.
  3. step (Optional) – integer value which determines the increment between each integer in the sequence.

It returns an empty sequence if the stop is negative or 0.

Let us see the range() method’s example.


app = range(5)
for data in app:

Now, see the output in the console.

Python Range Example | range() Function Tutorial

The range() function returns the sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at the specified integer.

Now, let us add a start and stop parameters and see the output.


app = range(6, 11)
for data in app:

So, here we have started from number 6 to 11. But 11 here means 10 because it starts from 0. See the output below.

Python range() Function Tutorial

Let see one another example in which we add the third parameter step and see the output.


app = range(6, 21, 3)
for data in app:

So, it will print every third integer between 6 to 21.

Python Range Example Tutorial

Create a list of an even number using range()

Let us create a list of an even number using the range() function.


app = range(6, 15, 2)
appList = list(app)

It will give the following output.

Create a list of an even number using range() function

Provide negative step in range() function

Let see the scenario where we provide the negative argument of step and stop in range() method.


app2 = range(2, -15, -2)
appList2 = list(app2)

Provide negative step in range() function in Python

In simple terms, the range() function allows you to generate the series of numbers within the given range.

It entirely depends on how many arguments you pass to the function, is it positive or negative arguments or you can decide where that series will begin and end as well as how big the difference will be between one number and the next by defining step parameter.

Finally, Python Range Example is over.

  1. Dasagriva says

    Useful example on using range.

  2. Madeleine Collard says

    Can I add exclusions to the range? For example for the range(4, 2024, 4), could I exclude the numbers 400, 800 and 1200 from the range? If yes, how?

    Thank you!

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.