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
For loops work their way over an iterable. Learn how to use them
Learn More
- Python Wiki - ForLoop (Immerse yourself, don't worry about understanding it all!)
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
The while loop is wonderful for
condition based looping.
0:00
While this condition is true,
run this code.
0:04
There's another type of looping
that I'd like to show off here.
0:06
And it's for when you want to
iterate through a set of values.
0:09
For instance,
think back to our banner example,
0:13
that we used to talk about strings.
0:15
And we can actually loop over
each letter in this banner.
0:17
We can say for each letter in this banner,
run some code.
0:21
Hey, let's do that in the shell here.
0:25
I'll set up the banner object.
0:27
Banner equals Happy birthday.
0:30
So, the keyword is for and
we're gonna say letter in banner.
0:36
Reads very nice, doesn't it?
0:43
So for each letter in the banner,
I'm gonna press enter and
0:45
you'll notice that there's three
dots now instead of the chevrons,
0:48
instead of the three greater
than signs three dots.
0:51
And that's because it's waiting for
us to finish this block of code.
0:54
So I'm going to go one, two, three,
four and we'll print letter.upper.
0:57
So what's happening, is each time through,
it's setting that letter variable.
1:03
And now, I still have dots because
it's waiting for me to finish.
1:10
So, here we go.
1:13
Happy birthday.
1:14
All capitals.
1:15
What happens here, is that each time
through the loop, each one of these loops,
1:16
this letter variable is set and you can
use it in the body of the function.
1:21
The loop runs through each item on
this side of the in keyword and
1:28
creates a new variable here
on the left side, that value.
1:33
The right side of this in key
word has one requirement.
1:38
It must be iterable.
1:43
Now I find it helpful to break
that word iterable down to
1:45
let's be able to be iterated.
1:48
Cuz what we are doing here is looping
through or accessing each element.
1:50
We're iterating through the values.
1:55
Now strings have been
designed to be iterable.
1:57
Each step through the loop provides
the next letter in the iteration.
2:00
There are also many types in
Python that are iterable.
2:04
And Python allows you to create your
own types that you can also iterate on.
2:07
We'll get to more iterable
types here in future courses.
2:12
Check the teacher's notes for more.
2:14
Now, do you see the difference
between the while and for loops?
2:16
For just runs from the start
to the finish of an iterable.
2:20
It runs through each and
every value it's given from the iterable.
2:23
With the addition of functions and
loops to your tool set,
2:28
you are now ready to write some
pretty powerful applications.
2:31
You might not believe me yet but
2:34
the skills that you've picked up thus far,
input and output, conditional branching,
2:36
math, exception handling, looping and
functions, are the foundations for
2:40
almost all of the rest of the work
that you'll do in programming.
2:44
There's always gonna be more to learn.
2:48
But you're now armed with quite
an arsenal of solutions for
2:50
just about any problem
that you might encounter.
2:52
I get it though.
2:55
It's hard to see how to combine all the
different skills that you just picked up.
2:56
Let's do this.
3:00
Let's wrap this course up with
a little simulation that will produce
3:01
some working software that utilizes most
of your new found Python super powers.
3:04
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up