In this lecture and the next one, we will have a brief look over the range data type. And to make things even more interesting, we will start with a quick comparison with the meaning of range in the previous major version of Python, namely Python two. For this, I will open up quotes counter.org, which is an online Python two interpreter, and I will use the range function by passing the value 10 as an argument in Python two, what range does it it generates a list of integers starting at zero and going up to but not including the value in between parentheses, in our case 10. So let's try print range of 10. Let's hit play. And the result is a list containing the elements from zero to nine.
As you can see, the range function has a default starting point at zero and the default step of one Since we get these consecutive integers returned, but don't worry, these can be customized as well. For example, let's start our list at five instead of zero. To do this, we just have to enter a new argument, also called start in between parentheses. So let's try print of range five, comma 10. And let's hit play again. This time we have the elements starting at five up to but not including 10.
Next, let's assume that we don't want consecutive numbers being generated. Instead, we want to also have a step up to, all you have to do is to enter the step you need as the third argument inside parentheses. So let's try this. Let's say print range. starting at zero, up to but not including 10 with a step of two. Let's hit play again.
And indeed We have 0246 and eight returned four Finally, it's worth mentioning that we can also use negative values for the start stop and step arguments. So for example, let's say we have print of range, starting at minus two, up to but not including minus 10 with a step of minus two, and of course, this returns minus two minus four minus six and minus eight. As you can see, with a step of minus two, I think this result was pretty easy to guess. Furthermore, in Python two, we also had the x range function available. Let me type this in. So x range, when given an argument, let's say 10.
Both range and x range ultimately return the same values zero to nine. However, the difference late in what data type does each of these functions return? The range function returned a list as we've just seen, on the other hand, the x range function returns an iterator. We will talk more about iterators in another chapter of this course. But for now, just keep in mind that the range function generates all the elements from zero to nine, as soon as it is executed, whilst the x range function produces an iterator that pops out each value one at a time, only when we tell it to do so. Why is that so important?
Well forget range of 10. And imagine you have a range of 10 trillion, that would generate a gigantic list starting at zero and up to 10 trillion Am I right? more elements means more memory being used. And usually you want to keep your programs as optimal as possible. That's why sometimes the x range function saves the day, or at least it did in Python two x range generates the same numbers, but only when we iterate over the object it generates. In conclusion, you should keep in mind that all this was valid in Python version two only.
In the meantime, Python three renamed the x range function to range The original range function was deprecated. But more on that in the next video