Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

The Basics of range()

In the previous lesson, you learned how to iterate or loop over a collection of data in the form of a list. That was great for your small list of names, but what if you want to generate a list of numbers? To do that, you can use the built-in range function.

There are three ways you can call range():

  1. range(stop) takes one argument.
  2. range(start, stop) takes two arguments.
  3. range(start, stop, step) takes three arguments.

00:00 In the last video, we learned how to iterate, or loop, over a collection of data in the form of a list. That was great when we had a list of names, but what if we want to generate a list of numbers? To do that, we can use the built-in range() function.

00:21 The range() function is used to generate a list of numbers within a given range. For example, calling range() with an argument of 100 will generate a list of numbers from 0 to 99. And as you can imagine, that’s a far better approach than creating a new list and typing out each number manually.

00:47 There’s a few different implementations of the range() function that we can use, and each one takes a different number of arguments. The one we just saw took one argument, which was used as the stopping value.

01:03 That meant that it started at 0 by default, incremented by 1, and stopped right before reaching the value we supplied.

01:13 If we want to change the starting value, or incrementing value, we can do that too. This implementation takes a starting value and a stopping value and increments by 1, the default.

01:30 This last implementation takes a custom starting, stopping, and incrementing value, so we can make it count by 2s, for example. It’s important to note that when working with the range() function, the starting value is always inclusive and the stopping value is always exclusive.

01:53 That means that when Python starts generating that list, it will start with the value you provide for the start, or 0 by default, and then it will continue generating new values up until but not including the stopping value.

02:11 This is done because indexing starts at 0, and so naturally, if we wanted to generate a list of, say, five numbers, we don’t want to get 0, 1, 2, 3, 4, and 5, as that would actually be six values.

02:30 Let’s get some practice with the range() function.

02:33 I’m here in Visual Studio Code in a new Python file. All I’m going to do is use the for loop we saw before. But this time, I’ll use the range() function instead of specifying some list to iterate over. This works because, behind the scenes, the range() function actually generates a list that the for loop can use to iterate over.

03:00 The first implementation I’ll use is the simplest one, where we just specify the stopping value. That looks like this. for i in range(3): print(i).

03:16 Before, it made more sense to call this temporary variable name because it represented each name in our names list, but here we are simply iterating over a list of incrementing numbers, so we’ll call it i.

03:31 That’s a very common practice in almost any programming language. Take a moment to pause the video and see if you can predict what the output of this program will be.

03:44 Remember, this basic implementation of the range() function defaults to starting at 0 and incrementing by 1, and it stops at 1 less than the value we supplied.

04:00 I’ll run this,

04:03 and we get 0, 1, and 2 in the output—exactly what we were expecting. The second implementation specifies an inclusive starting value, an exclusive stopping value, but maintains that default incrementing value of 1.

04:22 I’ll change my code here so that we start counting at 1 and stop right before hitting 8. I will run this code, and we see that now our list is starting at 1 and ending at 7. Again, just to drill this point home, the starting value is always inclusive, but the stopping value is exclusive.

04:49 The last implementation will allow us to specify the incrementing value by setting the step. By default, this is 1, which is why our lists were incrementing by 1.

05:03 I’m going to change all of the arguments in our range() function so that now we start at 3, stop at 16—or really, 15—and step by 3.

05:17 We can step by whatever we want, except for 0. If we pass in 0 for the step, we’ll get a ValueError.

05:26 Now I want to do something a little bit fancier. First, I’ll compute a quotient, which is the result of a division operation. I’ll say quotient = i / 3.

05:44 Now we have to print something, and so I’ll say print() and then the f-string f"{i} divided by 3 is {int(quotient)}".

05:58 This call to the int() function will convert the quotient to an integer, which will knock that .0 off the end. We could have also done this by using integer division instead.

06:14 And there’s our output! One important thing to note about the step function is that it will only go as far as the stopping value, but no further. To show this, I’m going to change the arguments in the range() function to 3, 100, and 25.

06:35 Start at 3, stop before 100, and increment by 25. Now pause this video again and take a guess as to what the output will be.

06:48 Remember, we can’t go past 99.

06:54 I’ll run this program, and we see 3, 28, 53, and 78 outputted. As you can see, it started at 3 and incremented by 25 on each iteration, but it stopped at 78 and that’s because 78 plus 25 is 103, which exceeds the maximum value of 99, and so it’s not added to that list.

Avatar image for viktor471

viktor471 on Dec. 24, 2021

About range. It returns range object that behaves like generator object

Become a Member to join the conversation.