Range function

The range() constructor generates numbers. This is very useful when creating new lists or when using for loops: it can be used for both.

In practice you rarely define lists yourself, you either get them from a database, the web or generate them using range().

Related course: Complete Python Bootcamp: Go from zero to hero in Python


Lets say you want to create a list of 100 numbers. Writing them out by hand would be very time consuming, instead use the range function:

Python starts counting from zero. Now what if you want to count from 1 to 100?

range function

A third parameter defines the step size, by default its one. Range can be used in a for loop:


Try the exercises below