Welcome back. I previously showed you
the accumulator pattern iterating over a sequence like the numbers from one to 10.
Python actually provides a function for us called the range function.
It takes as an input,
a number and what it produces as an output,
range of five will produce a sequence zero,
one, two, three, four.
So, the sequence it produces has the same number of items as the value that we passed in.
The first element will be zero and then we'll
get all the numbers up to but not including five.
If I say range of 10,
I'll get the integers from zero up to nine.
Now, there's one subtlety to this.
The range function in a real Python implementation doesn't actually produce a list.
It produces what's called an iterable,
something that will generate the numbers zero through
four when we iterate through them in a four loop.
So if I, say for I in range of five it will print out the numbers zero,
one, two, three and four.
If I actually want a real list,
I would have to cast it.
I would take range of five and pass it to
the list function which would turn it into a real list.
Now, in our Runestone environment,
we cheat a little bit and we actually have range of five producing a list object.
So, in the Runestone environment,
you don't strictly speaking have to do this casting of the output to make it a list.
But it's a good idea to get into the habit of doing things like
lines 12 and 13 because in a full Python environment,
if you really wanted to have a list you would have
to take the output of range of five and
pass it to list to cast it into being a real list.
One other thing I want to show you here and then
we'll see the output to check everything we've done.
Range, takes an optional second argument.
So, it actually takes several additional arguments and
we'll have a chance to see what impact this extra argument has.
So, let's run it. Just to match things up here we're just
printing that its range of five and then
lines four and five are producing these numbers zero to four.
That's the same as saying range starting at zero and going up to five.
But if I said range of one five and we'd get something a little different.
Now I would get starting from one up to but not including five.
There's actually an optional third argument that can say
you wanted to increment by a certain amount,
but we won't go into that detail.
Now as I was I was mentioning in the Runestone environment,
range of five comes out as an actual list.
In a full Python interpreter,
it wouldn't be an actual list object unless you first
passed it into the list function to cast it.
The advantage of having the range function,
is that we don't have to write code like on line
one to ask for all the numbers between one and 10.
In fact I'm going to take you back to our code for
the accumulator pattern and show you how it could've been a little simpler.
So, instead of saying,
"nums equals that list",
I could've said, "nums equals range of 1, 11."
I would have gotten the same result of 55.
The range function, will show up as a handy little utility for us throughout
the rest of the specialization and throughout
the rest of your time as a Python programmer.
But keep in mind, the range function can just take
one argument and then
it would give you all the numbers from zero up to but not including 11,
or it can take two arguments where you specify the first item and the last.
So, three to 11 is going to give us less than
55 because we're not including the one and the two.
So, we get 52 instead.
For the range function, takes a number and it gives you
back a sequence like this list of numbers from one to 10.
We'll see you next time.