Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Another iteration technique that Pythonistas can use is iterating with ranges.
Welcome back.
0:00
Now that we've tackled enumeration, we'll
spend this video talking about ranges.
0:02
Ranges are how we iterate
over numbers in Python.
0:06
They're commonly used when you wanna
repeat a behavior a certain number
0:09
of times.
0:12
But this behavior's not necessarily
tied to elements of a sequence.
0:13
In fact, iterating over Python ranges is
very analogous to the traditional for
0:16
loops you'll find in other
programming languages.
0:20
Let's say we wanted to have a for
loop that loops ten times.
0:23
Knowing what you know so
far about iteration,
0:26
you might try something like this.
0:29
Let's see what happens when I run this.
0:37
Okay, we're getting a TypeError here.
0:46
It says, 'int' object is not iterable.
0:48
This is telling us that 10 is an int,
or integer, and it's not iterable.
0:51
It's not a sequence, so we can't
loop over it like we would a list,
0:55
a tuple or a string.
1:00
So what do we do?
1:01
We use a range.
1:02
Ranges are the Python way of creating
an ordered, iterable sequence of numbers
1:04
with a starting number, stopping number,
and a step, or increment value.
1:08
Range is a Python method, like enumerate.
1:13
It takes three arguments,
start, stop, and step.
1:15
Start is the starting index.
1:19
In other words,
at what integer should the range begin?
1:20
The default value is 0.
1:24
Stop is the ending index,
the number at which the range ends,
1:25
you must specify this value.
1:29
It's important to note that ranges
are exclusive of the stop value.
1:31
Finally, step represents how
much the index increases or
1:35
increments from the previous number.
1:39
The default is 1.
1:40
Let's start exploring
a range here in Workspaces.
1:42
Feel free to follow along with me.
1:44
You'll have an opportunity to try
on your own in just a little while.
1:46
We'll pick up from our previous example.
1:49
To iterate over the numbers 1 through 10,
1:51
we'll change this incorrect
code to use a range instead.
1:53
0 is our start value,
10 is our stop value, and
2:02
1 is our step or increment value.
2:06
Let's save and run this.
2:09
You can see that the code printed out
all the numbers between 0 and 9, but
2:15
it didn't print 10.
2:19
Well, why is that?
2:20
It's because ranges
are exclusive of stop values.
2:22
Meaning the sequence returned from our
call to the range function began at 0 and
2:24
each element in the sequence increased by
one, stopping right before it got to 10.
2:29
But because the range began at 0,
it still looped 10 times.
2:33
This syntax for
this example is a little verbose.
2:37
Luckily, Python is smart enough to make
a lot of assumptions on our behalf.
2:40
When it comes to ranges, Python will
always assume that we want the sequence to
2:43
start at zero and increase by
one unless we tell it otherwise.
2:47
This means that the example we just
wrote can be shortened to this.
2:52
When you create a range, feel free to
only pass the stop value unless you need
3:00
a start or
a step value that defers from the default.
3:04
For instance, say we wanted this
range to start at 5 instead of 0.
3:07
In that case, we would again have to pass
a start value before the stop value.
3:11
I've left out the step value here because
I'm fine with using the default of 1.
3:19
Let's see what this looks like.
3:23
Cool, all right, take a moment
here to try this on your own,
3:31
pause the video after these instructions.
3:34
Open the attached workspace and
down in the terminal,
3:36
iterate over a range that starts and
ends at any two numbers of your choosing.
3:39
Then take some time to change the start,
stop and step values and see what happens.
3:43
For example, what happens if the start and
stop value are the same?
3:49
What if the step value is two or
five instead of one?
3:53
What if the start value is
higher than the stop and
3:56
the step value is a negative number?
3:59
When you've taken time to
play around with ranges,
4:02
come join me in the next stage to
learn about sequence operations.
4:04
You need to sign up for Treehouse in order to download course files.
Sign up