2.2 For/Else - IP

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

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