>> Hello everyone and welcome back to Intro to
Python here on Cybrary OnDemand.
If you're watching this video,
you're watching Lesson 2,
For and Else in Python.
Now, we're going to have two main objectives in this.
We're going to learn how to use for loops in Python,
we're going to learn about the very
special and very unique relationship
between for and else in Python.
Which is something a little bit different,
it's not really something that you'll have
seen in a lot of other languages.
We're going to just demonstrate that.
As before with our if statements,
we're going to open up our interpreter and we're
going to work from that at first.
I have it written in a script
that I'm going to show you in a little bit.
For now, we're just going to try it out and interpret it.
If you'll recall, we constructed an if statement,
was if conditional, and then our code.
For a for loop,
you're going to do something pretty similar.
It's going to be for condition, and then your code.
For example, one of
the most commonly used types of Python loop,
for i in range 10 print i.
Now what's going to happen here is it's going
to increment 0 through 10.
But, and this is important, I want to address it.
Range in Python is non-inclusive,
which is to say it's going to be the number 0,
1, 2, 3, 4,
5, 6, 7, 8, and 9, but not 10.
The reason for that is because we're
giving it the argument of 10,
the assumption is that you want 10 numbers,
not that you want to actually get to
the specific number of 10.
We're going to print i here, we're going to see.
Now, you'll recall from our lesson on for loops
in the abstract when we're not looking at Python,
all that's happening here is that it's
setting i equal to 0,
and then it's just running through that series of
numbers until it reaches the condition.
Here the condition is going to be 10,
and of course it doesn't print 10
because the actual for loop,
if you were to write it in pseudo
code will be constructed,
for i less than 10 print i.
That's the pseudocode implementation
that's happening here, the general idea of it.
What we're doing is we're just
going through all of those numbers and printing them.
That's the first type of for loop,
but there is another type that we addressed in
our fourth video in Module 1,
and we're going to clear our screen here
just a little bit and clean that up.
The best way to demonstrate that is by creating a list.
We're going to say l1 is a, b, c, d, e, f,
g. Then we're going to perform a for loop against that,
so for item in l1.
Now I want to note that item right
here is actually not a mandatory word.
You can use pretty much any word right there,
as long as it's not a variable that
already exists in your code or a Python keyword.
It could be for anything like that,
some ridiculous variable name.
Generally speaking, you're going to want to
use a simple one just for
the sake of easy code readability
and understanding what's happening.
Usually you'll see for example,
for letter in sentence is a common one or
for number in list, whatever.
We're just going to do for item in one print item.
Now this is very similar to our for i in range blank.
What's happening here is that we're just saying,
this list right here exists.
It is some iterable object,
a listed dictionary what have you.
I just want you to go through all of
the characters and all the values in that list,
and perform some logic which in
this case is just printing that value.
We're going to run that for loop
and we're going to see we print a, b, c, d, e, f,
and g. That's the basics
of how a for loop works in Python.
It's pretty straightforward.
It's exactly like what we talked about when we're talking
about for loops in the abstract in Module 1.
I mentioned, however, that
our other objective is learning how to
apply else to for loops in Python.
That one's a little bit trickier.
For that to work, you need to understand
what's called a break statement.
Again, we're going to be using l1,
which you'll remember is a, b, c, d, e, f,
and g. We're going to say for item in l1 if item is,
we'll say for this one d,
and I use single codes just to
keep from getting confusing.
If item is d, print found it.
Sure enough, you can see that it's going to go through
our list and say is a d?
No. Is b? No. Is c?
No. Is d? It sure is.
It's worth noting that this keyword in Python is,
works very similarly to the equal sign,
but it's not identical.
That loop could also have been constructed as for item
in l1 if item equals equals d, print found it.
That's going to work exactly the same way.
We're not really going to talk about the main difference
between is and a double equals sign in this video,
it's a slightly more advanced concept in Python,
so we're not going to spend any time on it.
Anyway, the point is, that it ran through
this list until it found the character was looking for.
However, there might be
a condition in which all you want to do is find
the character you're looking for and
stop execution as soon as you found it.
For item in l1 if item is
c print item and break.
What break does, is what it sounds like,
it breaks out of the loop.
If you reach a break statement in those loop,
it's going to say, "We're done executing,
we have reached our objective,
you don't need to examine the rest of this list,
you don't need to examine the rest
of the possible options."
Here you can see it returns c. Now,
this list isn't large enough for me to demonstrate
to you exactly how useful that can be,
but understand for example,
if you had a list of items that was a
1,000 items long or 10,000 items long,
which is actually possible,
using this break statement would
make your code substantially faster and more
efficient because of the fact that you stop execution
once you reach what you're looking for.
Now that we understand what a break
statement is and what it's used for,
we can start to apply an else.
That's interesting. For item in l1,
if item is z print item, and break.
Else, now you'll note here that else is
on the same level of indentation as for.
The same way that when you use else with an if statement,
it goes on that level of indentation.
If we were to tap this in,
it would now be an else that is
evaluating whatever happened in this if statement.
Since it is where it is,
it's going to evaluate against the for loop.
Remember that Python is white space delineated.
Else print no zed found.
Now what's happening here, what else
actually does when you use it with a for loop.
Instead of with an if where it would evaluate,
did that condition evaluate to true?
Instead it's going to say,
was a break statement reached?
Did at some point in the for loop?
Did the code reach a break statement?
Note that this code will only
reach a break if it finds the letter z in the list,
which of course it isn't there.
It's going to activate our else.
Sure enough no zed was found.
For item in l1,
it goes a, b, c, d, e, f, g,
none of those are z, therefore we never break,
therefore we run our else statement.
That's all there is to it. That's for loops in Python,
you can see the relationship between for and else,
you can see how for loops are used.
I also just for real quick demonstration,
do have that written out in this,
let me check the file, yes,
fantastic, in for else.py.
You can see here exactly what I just demonstrated.
Though this is actually the letters a through
h. We do have some exercises for this.
If you look over to the side, you'll see a list of
for loops that you can try and implement yourself.
Additionally, if you have access to
our Insider Pro program,
you will be able to perform some of these
for loops in our next tech lab.
If you don't have that, we'll still have
the assignments there for you to do on your own.
But I do highly recommend
you make use of that lab because it's
just absolutely spectacular and very easy to use.
That's going to be all there is for this video.
As always, I am your instructor Joe Perry.
You've been watching Intro to Python
here on Cybrary OnDemand.