1.6 While Loops - 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 and welcome back.
00:00
It is intro to Python here on
00:00
Cybrary OnDemand with your instructor, Joe Perry.
00:00
This is going to be Lesson 5,
00:00
where we're going to talk about the programming basics
00:00
of while loops.
00:00
In the last lesson, we discussed for loops.
00:00
We discussed the concept of looping,
00:00
of implementing a piece of logic that's
00:00
going to be used over and over again.
00:00
In this lesson, we're going to take
00:00
that same general concept and
00:00
we're going to use it to understand while loops.
00:00
We're going to also understand the relationship
00:00
between while loops,
00:00
if statements and for loops.
00:00
Remember from the last lesson,
00:00
the for loops repeat
00:00
a given sequence until a condition is met,
00:00
while loops do the exact same thing.
00:00
The distinction that really
00:00
is most important is that what while loops
00:00
do is that they just evaluate
00:00
some ongoing phenomenon, some ongoing condition.
00:00
It can be used the same way as a for loop,
00:00
but often it's used for other purposes.
00:00
An example here is while it is dark outside,
00:00
tint to the computer screen red.
00:00
If that's confusing to you,
00:00
there are a lot of applications that do
00:00
that because it's better for your eyesight.
00:00
If you're someone like me who
00:00
spends all day staring at a computer screen,
00:00
you're going to take every little bit you can get.
00:00
But the idea here, like I said,
00:00
is that while loops are used to implement
00:00
against an ongoing phenomenon, an ongoing condition.
00:00
Oftentimes when you write a program that's intended to
00:00
have user input, for example,
00:00
an Internet browser or a word processor,
00:00
or really most of the applications you use day to
00:00
day, generally speaking,
00:00
somewhere inside of those applications,
00:00
there's what we call the for loop,
00:00
which is just a simple while loop that is waiting
00:00
for signals and processing
00:00
those signals based on your input.
00:00
Signal handling can get complex.
00:00
We're not going to talk about that actually
00:00
all in this particular class,
00:00
that's going to come up during
00:00
intermediate and advanced Python,
00:00
however, and we'll talk about it pretty extensively then.
00:00
The point that I'm making here is that while loops are
00:00
often used for infinite loops or
00:00
used for loops that are intended
00:00
to last for a very long time.
00:00
While X is less than 10,
00:00
print and then increment x.
00:00
Now you can see that this does
00:00
the same thing that a for loop would've done,
00:00
but the difference here, one, is that we're
00:00
implementing the increment ourselves.
00:00
That's not happening in the while loop.
00:00
It's not happening in the internal logic of Python.
00:00
The other thing that's important to note here is that
00:00
instead of using for,
00:00
where we're going to evaluate for each item,
00:00
what's actually happening here
00:00
is just that we're repeatedly
00:00
evaluating the Xs less than 10,
00:00
and it doesn't necessarily change.
00:00
Like I said, you can have an infinite while loop and it's
00:00
entirely possible to construct a while loop where
00:00
you just don't increment the X or you
00:00
don't increment whatever value or you don't
00:00
change whatever signal and
00:00
therefore the while loop just keeps executing.
00:00
While X is less than 10,
00:00
we're going to print it and we're going to increment it.
00:00
Of course, we're going to set
00:00
X equal to 0 at the beginning.
00:00
This is a case with while loops where you
00:00
will have to usually assign an initial value.
00:00
For loops, Python can often do that
00:00
for you based on the information,
00:00
while loops you usually have to do it yourself.
00:00
X is less than 10,
00:00
that is a true statement.
00:00
We're going to print it, we're going to
00:00
increment it and then we're just
00:00
going to repeat those steps,
00:00
looping exactly the same way we did with for loops.
00:00
Changing the information, performing logic,
00:00
and then evaluate it until finally X equals 10,
00:00
which in this case we are only looking for
00:00
a value X is less than 10.
00:00
We're going to say that it's false.
00:00
Obviously 10 equals 10,
00:00
it is not less than.
00:00
We're going to end
00:00
our execution and we're going to give back
00:00
control to whatever code comes next,
00:00
whatever part of Python code or
00:00
whatever part of your module is happening next.
00:00
Another example here that demonstrates a difference,
00:00
X equals the letter A.
00:00
This is totally pseudocode.
00:00
This wouldn't work in Python the way it's written.
00:00
But while X is not equal to Z,
00:00
the exclamation mark equals sign, it's a symbol.
00:00
Thank you. I lost the word.
00:00
It is a symbol in Python,
00:00
which means not equal to,
00:00
the logical inversion of equal to.
00:00
While X is not equal to Z,
00:00
X equals the next letter.
00:00
Then outside of this while loop as its own instruction,
00:00
we say print X.
00:00
You can see that I say here these are
00:00
separate instructions which are executed sequentially,
00:00
which means that this while loop is going to performance
00:00
behavior before the print statement ever happened.
00:00
It's going to look at the letter and say A,
00:00
no, that's not Z.
00:00
We're just going to cycle through A,
00:00
B, C, D, E, F, G,
00:00
all the way down until finally it gets to Z and it says,
00:00
oh, [NOISE] found it,
00:00
while loop is done.
00:00
Then it feeds to the next line of code,
00:00
which in this case is going to be printing that letter.
00:00
In a while loop implemented like this,
00:00
only the letter Z will ever actually print.
00:00
The while loop will execute this behavior
00:00
completely and then give control back.
00:00
A knowledge check for you,
00:00
a sample while loop.
00:00
N is going to be given the initial value of 100.
00:00
While N is greater than 10,
00:00
we'll print and then decrement.
00:00
I'm going to go ahead and give you just a few seconds to
00:00
see actually the question here,
00:00
how many numbers will be printed?
00:00
I'm going to give you just a few seconds,
00:00
pause the video, have a look,
00:00
see if you can figure out
00:00
how many numbers are going to be
00:00
printed. Let's go over it.
00:00
The answer is 90, the number
00:00
>> is 11 through 100 backward.
00:00
>> It's going to be 100, 99, 98, 97, 96.
00:00
All the way down, it will print 11.
00:00
It will then say N equals 10,
00:00
which of course is not greater than 10.
00:00
We're done executing, move on.
00:00
That's all there is for Lesson 5 which was while loops.
00:00
I'm glad you've made it this far with us.
00:00
If you have stuck with us
00:00
all the way from the course introduction in Lesson 0,
00:00
I've excellent news for you,
00:00
which is that from here on out,
00:00
we're going to spend very little time in our PowerPoint.
00:00
Almost all of our time from now on is going to be
00:00
spent in our Ubuntu 64-bit VM,
00:00
starting with Lesson 6,
00:00
which is the next lesson, Python basics.
00:00
We're going to talk all
00:00
about how you can create a script.
00:00
You're going to get right to your first program and we're
00:00
going to start spending all of our time in that VM.
00:00
From here onward, the slides are
00:00
pretty much going to be used to set
00:00
our objectives and to perform
00:00
our summaries and our reviews.
00:00
Thank you all for sticking with us.
00:00
Hopefully you've learned something so far.
00:00
Hopefully you're excited to start coding.
00:00
I'm very excited to start coding with you.
00:00
I look forward to having you-all back in our next video.
00:00
As always, I am your instructor, Joe Perry,
00:00
and thank you for watching this on Cybrary OnDemand.
Up Next