>> Hello everyone and welcome back to intro to
Python here on Cybrary on-demand.
As always, I am your instructor Joe Perry.
I'm going to be running you through today's lesson,
which is Lesson 3.
While Loops in Python.
Lesson 3 we only actually have one objective.
It's a very straightforward objective we're going to
learn to use while loops in Python.
The reason why we only need
one objection to the reason why this is
a slightly shorter lesson is because of
the fact that we already covered the concept
of conditionals and looping in our previous videos,
so here we're just learning
about a new implementation of that.
We're going to change to the correct lesson folder,
which I believe is Lesson 3. Yes, Lesson 3.
Fantastic. When you record videos for 12-14 hours a day,
it's really [LAUGHTER] easy to forget where you are,
so we've got Lesson
3 which did not update it at the top of the screen and
that's just going to bug me the whole video and now
because I drew your attention to it, it will bug you too.
Welcome to this wonderful, glorious pain.
Anyway, back to the lesson Python 3.
We opened our interpreter here.
Now you'll remember that what if we do
some if conditional and then code with for we did for
condition code while loop is going to be much
like an if statement as we talked about in
our looping lesson when
we were just talking in the abstract.
It's a very similar concept of,
it's basically just evaluating some Boolean value here.
Now, with for loops you were able to
do for i in range 10.
With while loops, you can't quite get away with
that because of the fact that
the Python interpreter does not
automatically know to insert
some information into that variable
so while x is less than 10,
print x is going to
break because of the fact that x is not defined,
so you have to do a little bit more legwork with
your while loop and
predefined whatever your variable is going to be.
In this case, we're going to say x equals 0,
while x is less than 10,
prints x, and then x plus equals 1.
This is the same thing as saying x equals x plus 1.
It's just a shorthand to help you
increment with a very little bit less code,
so while x is less than 10,
print x and increment.
Sure enough, you can see we print 0,1,
2, 3, 4, 5, 6, 7, 8, and 9.
Now I want you to have a look and really
noticed that incrementation piece right there.
Because if we didn't have that code,
if we just had x equals 0,
while x is less than 10, print x.
If we run this code right now,
it's never going to stop printing zero, because again,
the while loop requires you to do
a little bit more of your own work.
It doesn't really know how
to change that variable
automatically or how to interpret it.
We've got our while loop, but what if
we wanted to reach a specific piece of information?
For example, what if we wanted to do something
like taking a list apart piece-by-piece,
there's a useful function we're going to use here,
which is that lists name dot pop.
What that does is it removes the last item
from the list and prints it to your screen and again,
we'll talk more about functions
and methods in a later video.
But for now, we're just going to make use of that,
we're going to do while len,
l1 is greater than 0,
len is another function that's just going to tell
you how many items are in your list.
While the length of l1 is greater than 0,
while there are more than 0 items in the list.
l1.
pop. You can see
here that it's going to print 5, 4, 3, 2, 1.
But what if we didn't have
that evaluated quite the way we might want to.
What if instead, we had our infinite loop?
Sorry, a little bit of
dead air as I'm building this out,
programming on screen is always
an exciting endeavor because you
never know when you're going to mess something up,
while x is less than one.
For example, I just messed that up by not rebuilding
my list. There we go.
While x is less than one, l1.pop.
[NOISE] Now, this is a potentially infinite loop here
because we're never modifying x and again,
you'll see this happen pretty often in code where
there's some tight infinite
while loop that spins very quickly and it's just
designed to keep the program
running while it waits for some input.
But what we can do here is we can say l1 pop.
Then we can have an internal if internal condition.
If len l1 is 0,
remember double equals is to check the condition
rather than assign it. We break.
Make sure I've gotten right level of indentation.
I remember breaks from our last video.
Tell the loop, "Hey, we're done.
We've figured out the piece we need.
We've executed the code that we want to
execute whatever break the loop."
Instead of throwing up an error
or having some infinite loop,
it says, "Okay, we've
popped all the items out of that list,
we're done, we can leave."
So that's really all there is to using
while loops in Python.
Again, they're implemented very similarly to for loops,
except for the fact you have to do
a little bit more legwork with them.
I hope you enjoyed this lesson. I hope you
find it valuable as with
if for and to basically everything else in Module 2,
you can get some extra practice
over in our supplemental materials.
You can also work a little bit in our lab,
the wonderful lab provided by
these spectacular company NextTech.
You can jump into that,
work with the while loops, for a little bit,
and then come back because
in Lesson 4 we're going to learn
more about logical escapes in Python.
As always, I am
your instructor Joe Perry
and I just want to thank you again
for watching intro to Python here on Cybrary on-demand.