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