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