Flat_Eric Contemplating Data Type Conversions

Range()

range()

range() is a built in Python function that generates a sequence of integers. It is one of the most useful tools you will use in combination with for loops. Before we look at how to use it, here is the full signature:

range(stop)
range(start, stop)
range(start, stop, step)

So range(4) produces 0, 1, 2, 3. Not 4. This trips up a lot of beginners. The stop value is never included in the output.

range() is an Object

If you try to print range(100) directly you will not see a list of numbers. You will see this:

range(0, 100)

That is because range() returns a range object, not a list. It does not generate all the numbers immediately. It waits until you need them. This makes it very memory efficient. Instead of storing 100 numbers in memory, Python just remembers the start, stop and step and generates each number on demand.

Because range is an object it is also an iterable. That means we can loop over it just like a list, tuple or string.

Try the following in the box below:

  • print(range(100))
  • print(range(0, 100))
  • print(range(0, 100, 2))

Iterating Over range()

Now that we know range is iterable we can use it in a for loop. This is where range becomes extremely powerful. Instead of creating a list of numbers manually, we can just tell Python how many times we want to loop.

Try each of the following and run them one at a time:

  • for number in range(0, 100): then print(number)
  • for number in range(0, 100, 2): then print(number)
  • for number in range(100, 0, -1): then print(number)

When You Don't Need the Variable

Sometimes you want to use range to control how many times a loop runs but you do not actually need the number itself inside the loop. In this case it is common practice in Python to use an underscore _ in place of the variable name. This signals to anyone reading your code that the variable is intentionally unused.

For example if you want to print a message 50 times you do not need the loop variable at all:

Using _ is not a rule, it is a widely adopted convention. Python will not complain if you use a regular variable name and never use it, but _ makes your intention immediately clear to other developers.

range() and list()

We almost always see range() used directly inside a for loop, but it can also be wrapped in the list() function to convert the range object into an actual list. This is useful when you need to store or inspect the sequence of numbers directly.

Try the following in the box below:

  • print(list(range(10)))
  • print(list(range(0, 20, 2)))
  • print(list(range(10, 0, -1)))

We can also combine this with a for loop. Instead of iterating over the range directly, we iterate over the list that range produces. The result is the same but now you can clearly see the full sequence that is being looped over. Try the following:

  • for _ in range(5): then print(list(range(10)))

Notice that the same list is printed 5 times. The outer range(5) controls how many times the loop runs and the inner list(range(10)) generates the full list on each iteration.

Time to experiment!

Coding Exercises (VS Code) Instructions:

Exercise 1: Basic range()

Exercise 2: Counting Down

Exercise 3: range() and list()

Exercise 4: Using _

Exercise 5: range() with a Condition

Don't Forget to commit and Push!

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course