hey how's it going everybody in this
video we're going to take a look at the
else statement and what it means
whenever you have an else statement
after a loop whether that's a for loop
or a while loop now this is confusing to
a lot of people who are new to Python
because it's not as obvious what's going
to happen in this situation as it is say
if you have an if statement like I have
a little sample file over here with an
if statement and this is pretty
straightforward and we've seen this
especially if you're coming from other
languages you've seen this stuff before
you have so in this example we have num
equal three if num is less than two and
print this else print this and if I run
this code then you can kind of tell
what's going to happen num is not less
than two now that's not as obvious in
this example so we have this for loop
and then we have this else so what is it
that could trigger this else statement
whenever I first started python
programming I thought that had something
to do with whether you passed it an
empty list or something like that then
the else would execute but that's not
the case you see I have a list here that
is 1 2 3 4 5 and I have a for loop that
loops through all of those items in the
list and prints off that item then I
have an else de mayor that just prints
whether or not we hit that else
statement so if I run this code you can
see that my for loop did run so it
printed off 1 2 3 4 5 all separately but
then it also hit the else statement here
now that's pretty weird especially if
you compare it to an F else statement
because in an F else statement it's
always one or the other either this
block of code is going to get executed
or this block of code is going to get
executed but here we can see that both
got executed now to better understand
what's going on here one of the Python
core developers at PyCon one year was
given and talked and he said that in
situations like this he really thinks
that the else statement should have been
named no break and if you think about it
as being called no break then it makes a
lot more sense so what do I mean by
thinking of it as being called No
break well if you're familiar with the
break statement what it does is whenever
you're within a loop and your code runs
into one of those break statements then
it will break out of the loop so for
example if within this for loop here if
I put if I is equal to three then break
now when I do this and then I run the
code you can see that it printed out one
two three but it did not print the code
from this else statement now the reason
it didn't print this code from the else
statement is because it hit this break
here so if you think about this as being
called no break then it makes more sense
because it's saying if no break was hit
then execute this code so for example if
I was to put a six here instead now it's
not going to hit this break so if I run
this then you can see it went through
all the items in the list it didn't hit
this break statement so it bring it off
the else or print it off what was within
the else I mean so this is also true for
wild statements here so I have a while
loop here so while I is less than five
print I and then incrementing I by one
every time and then else print that I
hit the else statement so if I run this
code as is it prints off one two three
four five and then the while no longer
meets that condition and then it also
prints the else because there was no
break that was executed to break out of
this loop now I've got this commented
out here if I put this in and then you
can see once I equals to three it's
going to hit this break and then it
shouldn't execute this else so if I run
that code then you can see it printed
one two once it got the three it broke
out and didn't execute anything else so
hopefully now you better understand that
control flow whenever you see an else
statement after a loop now if we want to
see this in a practical example so like
when would you use something like this
well we have this little sample file
here where I have this function called a
find index and so with in this function
I'm just going to pass in a list to
search and then I'm going to have a
target and within the function I'm going
to loop through all the indexes and
values and if the value within this list
equals the target that I'm searching for
then I'm going to break out and then I'm
going to have an else statement here
which you can think of as a no break if
it doesn't hit this break then I'll
return a negative one and then if it
gets to the end here then it will return
I which will be the index so now let's
take a look at an example of this
function so I have a list here with
three names Korie Rick and John and then
index location I set equal to this
function find index I pass in the list
of these three three names and then I
search for the name Rick and then this
little print statement down here just
prints out the index that we found
running our function so let's go ahead
and run this now you can see it says
location of target is index 1 so 0 1 so
it found it right there so let's go
ahead and and kind of walk through this
so we passed in this list and this
target and you can see that it went
through the loop and as soon as it found
the value was equal to the target it
broke out and because it broke out it
didn't execute this else statement which
would have returned negative 1 and since
it didn't return negative 1 then it got
down here to return I which was the
index of 1 now if I search for something
that's not in that list and I run that
then you can see it returned negative 1
because it went through this entire list
and it never met this conditional of if
the value is equal to the target it
never met that because Steve isn't in
the list so it never hit this break
statement so then it executed this else
which you can think of as a no break and
it returned a negative 1 so that's just
one small example of where you would use
something like this but as you keep
programming in Python you're going to
find a lot more use cases where this
really comes in handy and it's a it's a
good thing to know so hopefully I
cleared up any confusion that you had
about the else statement whenever you
see it after a loop but if you have any
questions just ask in the comment
section below
be sure to subscribe for future videos
and thank you guys for watching