Let's now go back to our trusty replit. And I'm gonna give you two examples.
One for the for loop
and another for the while loop.
So
in order to create our for loop, let's first of all, create the list in
this case, right now, I'm going to provide the name of the list as names. OK?
Equals
and then I'm gonna have my brackets in here and let's add
some names. So I've got uh Alex,
I've got uh Nancy
and I've got a James
and let's say last, but not least
I
have got
uh Violetta.
Honestly, I just came up with that name,
Violetta. OK.
So we have our list right here of four different names, right? Alex, Nancy James.
And then Violetta,
one thing I can do right now is we can create a variable
that will represent each item in the list.
Kind of like how we've been assigning values to variables like X equals four,
Y equals seven.
I can now come in here and then simply say
four,
OK? And now I will say four X
in
names.
OK?
What this does right here and let me just add the colon right? There.
So what we've done with this statement here is that we're saying, OK,
we are going to use X to represent each
name in this list. So X will be equal to Alex. That will be the first loop, that
one,
the second loop X will not be equal to Nancy. It will
run, the third loop will not be X equals to James.
And then finally, the very last loop will be X
is equal to violet. And that's basically what this does in here.
So we're saying for X in names
and now we have to tell the program. OK. What should we do? Well,
we should print out
the name
X.
OK.
I am going to run
and there you go,
Alex,
Nancy, James and
Violetta.
But what if I wanted to spice this program up
just a little bit and let's say something like,
you know, good morning and then the name of that person.
So we'll have Good morning, Alex, good morning, Nancy, good morning James.
And of course, good morning
Violetta.
How are we going to do this? Well,
inside of the print function,
I am going to add a string that will first of all say
good
morning,
okay.
And then we're going to have the comma which by the
way is just part of the actual print out itself.
It's not part of the actual program.
If you know what I mean, it's not,
it's part of the string basically is what I'm trying to say, we can
do so without the comma as well.
In fact, you know what, let me remove the comma just so I don't confuse you.
So I'm gonna say good morning.
Ok?
And then
I am going to add a space
because we want to be space between the actual string.
Good morning and then the name of the person.
That's why I've added the space right there.
Ok? And then right here,
I am going to add the plus sign
and then X
so basically attaching
the string. Good morning
to the name of the person.
Let's run the program
and there you go.
Good morning, Alex. Good morning and good morning, James. Good morning, Violetta.
Now, if I remove this space because I want you to understand this very well.
If I remove this space right now and I run the program again,
you can see right now that it's all jumbled up,
there's no space between morning and then the name of the person.
That's why once again,
I added the space right here between the G and the closing quotation marks.
So
one more time
and there you go.
This is an example of a very simple for loop.
First of all, we created a list of names, we assigned this entire
list right here to the variable names.
And then we now need to create another variable that will represent each item,
each name in this list.
So we said for X in
the list called names.
And now the function print good morning
plus the name of the person in that list.
This is an example of your for loop.
Now,
what I'm gonna do is
we're going to create a while loop now to do this.
Let's come down here and yes, we can run
multiple programs and functions within the exact same file. All right.
So
what I'm gonna do right now is I'm going to create something called a counter.
OK? So counter
equals zero.
Now, what exactly am
I doing here? What is this counter?
Remember that with while loops,
they will continue to run as long as a condition
continues to be satisfied.
That's the difference between a for loop and a while loop with the full loop.
We have a list of items, right? So
the loop will simply run through all the items in that list.
But a wide loop, there has to be some sort of a condition
that will be presented.
And then we're gonna tell Python that, hey,
as long as this particular condition continues to be satisfied, execute the loop.
So
the condition here right now is that while a counter
is less than, let's say three,
do you now see
we're saying, hey, as long as our counter,
as long as the number in here is less than three,
continue to do something. And you can see Python kind of already given us a hint
of where to go next. Well,
I'm gonna say print,
print and then
I'm going to add a text right there, the string.
Uh hello.
Ok.
And there you go.
Now what we're saying here is, start from zero.
Ok, start from zero and then check to see if zero is less than three.
If it's less than three, then print. Hello.
However,
we're not actually done.
Why?
Well, because we haven't specifically told Python
to
add
a new number to the counter. So
instead of zero, it will not become one
check to see if one is less than three and then print out. Hello.
We haven't told Python that yet because a loop can go either way,
we can either continue to add
or 123 to 0 or we could go the opposite direction and then instead of zero going to one,
it could be zero, going to minus one minus two minus three minus four.
And in that kind of scenario,
those numbers will always be less than
three and the condition will never be satisfied
or that it will always be satisfied rather.
And then that will crush the program.
So we need to tell Python explicitly that, hey,
once you've run the very first loop where zero is less than three,
add one to the counter.
So
I'm gonna come in right now and I'm gonna say counter
and then
plus equals one.
This is our way of saying OK, Python
the very first, the very beginning of the loop will be zero,
check to see if zero is less than three, if it's less than three print. Hello.
And then now add one to the counter. So it will not be one
at less than three. Python says. Yes, one is indeed less than three.
It's gonna print out. Hello. It's gonna add one again. So one will not become two.
Python is gonna check. Ok. It's two, less than three.
Yes, it's less than three print. Hello. And then it's gonna add one again.
So to now becomes three.
And then finally, Python will say that OK,
three isn't less than three because three equals three and then it will end
the loop.
OK?
Let's go ahead right now and run
and there you go.
Hello. Hello? Hello.
You can see right there that it stopped at three
loops because the very first loop will be for zero,
second loop will be for one third loop will be for two
if I came here.
And I said, OK, less than seven
run.
Now, you can see we have again, 40123456 hellos.
So
that's the difference between
the for loop and
the while loop.
You can see right now with the full loop, we have our list or our, our way
and then the loop will go through each particular
item in that way and it will do something.
But for the while loop, we need to first of all specify what's the beginning point
and then we will not provide the condition as long as this
equals this or this is less than this or this is this,
do something
and then either add one or remove one or just do something
to make sure that the loop will now actually begin to run.
That's the difference between the for loop and the while loop.
Of course, we're going to be working a lot more
with four and wide loops in this course. But thank you for watching.
I will see you in the next class.