Last Updated: Monday 8th December 2014

What is Python's range() Function?

As an experienced Python developer, or even a beginner, you've likely heard of the Python range() function. But what does it do? In a nutshell, it generates a list of numbers, which is generally used to iterate over with for loops. There's many use cases. Often you will want to use this when you want to perform an action X number of times, where you may or may not care about the index. Other times you may want to iterate over a list (or another iterable object), while being able to have the index available.

The range() function works a little bit differently between Python 2.x and 3.x under the hood, however the concept is the same. We'll get to that a bit later, however.

Python's range() Parameters

The range() function has two sets of parameters, as follows:


  • stop: Number of integers (whole numbers) to generate, starting from zero. eg. range(3) == [0, 1, 2].

range([start], stop[, step])

  • start: Starting number of the sequence.
  • stop: Generate numbers up to, but not including this number.
  • step: Difference between each number in the sequence.

Note that:

  • All parameters must be integers.
  • All parameters can be positive or negative.
  • range() (and Python in general) is 0-index based, meaning list indexes start at 0, not 1. eg. The syntax to access the first element of a list is mylist[0]. Therefore the last integer generated by range() is up to, but not including, stop. For example range(0, 5) generates integers from 0 up to, but not including, 5.

Python's range() Function Examples

Simple Usage

Iterating Lists

99 Bottles of Beer on the Wall...

With the following code:

We get the following output:

Brilliant! Finally you can see the true power of Python :). If you're a little confused, for reference see the Wikipedia article.

Python's range() vs xrange() Functions

You may have heard of a function known as xrange(). This is a function that is present in Python 2.x, however it was renamed to range() in Python 3.x, and the original range() function was deprecated in Python 3.x. So what's the difference? Well, in Python 2.x range() produced a list, and xrange() returned an iterator - a sequence object. We can see this in the following example:

So in Python 3.x, the range() function got its own type. In basic terms, if you want to use range() in a for loop, then you're good to go. However you can't use it purely as a list object. For example you cannot slice a range type.

When you're using an iterator, every loop of the for statement produces the next number on the fly. Whereas the original range() function produced all numbers instantaneously, before the for loop started executing. The problem with the original range() function was that it used a very large amount of memory when producing a lot of numbers. However it tends to be quicker with a small amount of numbers. Note that in Python 3.x, you can still produce a list by passing the generator returned to the list() function. As follows:

To see the speed difference between the original range() and xrange() function, you may want to check out this article.

Using floats with Python's range() function

Unfortunately the range() function doesn't support the float type. However don't get upset too soon! We can easily implement it with a function. There's several ways it can be done, however here's one.


To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.

  • jerome

    “It specifies that it will stop on the Nth number produced, where stop is the Nth number.”
    Which is wrong, considering the examples.

    • Jackson Cooper

      Thanks for pointing that out Jerome. I’ve updated the article to reflect the behaviour.

  • johndoe54321

    So this explains why “range(5)” evaluates to “range(0, 5)” and not “[0,1,2,3,4]” in python 3. Thanks for the explanation!

  • Caridorc

    my_list_len = len(my_list)
    >>> for i in range(0, my_list_len):
    … print(my_list[i])

    Don’t please…..

    You should use:

    for i in my_list:

  • emberstream

    Thank you so much! Trying to complete codecademy and translating everything into Python 34 as I go. The difference in the use of range() really stumped me for a bit. Thanks!

  • ramkay

    Shouldn’t the 10th line in the 99 bottles of beer read ‘So take one down’ instead of ‘So take it down’?

  • Bob Jones

    This will really help explain the range function to the kids at school. Thanks!

  • Suresh Dooly

    Am clear now on RANGE 😉 Thanks….!

  • uuuuuuuu


  • Moses

    Cool stuff
    you can try:
    … while i < stop – (step/2):

    … yield i
    … i += step
    On frange(..)

  • yDeepak1889

    can anyone help me…..
    how much times does following loop iterate
    for i in range(3,3,-1):

    i was thinking that here range function produce list like [3,2]
    but it’s not…working

    • kumar Shivam

      man ur starting and end index are same….how would it iterate
      starting : 3
      end : 3

      try range(3 , 2 , -1)

      • Abirbhav Goswami

        That too would not work. It would just give a range with 1 value. So if you use a for loop to build an array, it will just give [3]. You need to use range(3, 1, -1) since the 1 will be excluded but 2 will be added.

  • kumar Shivam

    this is a great place to start with python….with a whole lot of examples its too delicious.
    Thanx Jackson

  • Mike Litoris

    Is there anything i can do to include the last item in range?

  • bassem

    how to get user seatnumber and output result

  • jaywhy13

    Great explanation thanks!

  • satish jasti

    thank you

  • justanr

    range is a proper Sequence in Python 3, so it *is* sliceable.

    >>> isinstance(range(4),
    … True

    >>> range(4)[2]
    … 2

  • Paul

    how do i create a list like this [0.01,0.02,0.03…….5] in python

    • a


      • JP

        All parameters need to be integers. Try
        list = [x / 100 for x in range(500)]

  • John Mason

    The “Iterating Lists” part is wrong. It actually just posts an integer value for the amount of indexes in the list, not an iterative listing of the index contents.

  • N V Susmitha

    gud explanation!!!

  • Raja Harsha Chinta

    Excellently explained ! Thank you

  • zack

    thanks mate i got confused when my teacher made us use ranges

  • vivek

    Super explanation….. Guys

  • Tracy

    Thanks! You explained quite well!

  • Qi Zhang

    Thanks for helping me.

  • Anshita

    I want to ask u that how the following code generates the output:

    astring = “Hello world!”;


    Please reply asap.