What I want to do in this video is show you that "for" loops
aren't the only types of loops you would use,
although they probably are the most common.
You could also use something like a "while" loop,
and maybe the while loop might be more intuitive,
at least maybe in the Python context -- you can pick.
So I'm going to write the same program.
It's going to do the exact same thing, but instead of using
a "for" loop I'm going to use a "while" loop.
So I'm going to still start with the "'sum' equals zero",
but I'm also going to start with "'i' equals zero".
So the "while" loop isn't going to automatically define
what my "i" values are, and it's not going to automatically
change my "i" values or me.
I'm going to have to do that for myself,
and I want to do the exact same behavior.
I'm going to leave my old code down here
so you can compare it a little bit, but then I'll delete it
because I don't want the same thing to be run twice.
So I'm going to set "i" equal to zero,
and this is, in case you're curious,
the last time in the for loop "i" is only a valid variable inside the loop.
Now I'm defining it outside of the loop,
so it's actually a global variable.
I'm defining it at the highest level.
It can be used anywhere inside of the program.
And so, over here, let's define a "while" loop.
So I'm going to keep doing what's inside of the loop clause,
and I'm going to do it while "i" is less than 10.
Remember: we want to do it for "i" equals zero, one, two,
three, four, five, six, seven, eight, nine: everything up to,
but not including ten (zero through nine).
So while "i" is less than ten,
do what I'm going to put in this clause.
Once "i" is not less than ten, or essentially once "i" is equal to ten,
don't do it anymore.
So it's going to have this same behavior as this right over here,
and what I'm going to do is I'm going to say,
"'sum' is equal to 'sum' plus 'i'",
and then I'm going to print sum.
So this is the exact same logic that I had when I wrote the "for" loop,
but I'm going to have to add one more line here if I don't want this thing to run forever,
because right here in the "for" loop you remember:
every time we run this clause here,
as we get out of the clause,
it will assign "i" to the next element in this list here.
Over here, we haven't defined how "i" should change,
so what we have to do is: in every loop, we have to increase the value of "i".
So we could say "i" is equal to "'i' plus one".
I want you to really think about how both of these are equivalent things.
"i" started at zero before I even entered into this loop,
and then "sum" started at zero for either of these.
So you say "sum" is equal to sum plus "i".
Well, that's the same: "print 'sum'".
Now over here, I will then, once we go through this loop in the "for loop",
be assigned to the next element in this list,
which is "one". Over here there's nothing to say to do that,
so we're just going to say "i" is going to be the previous "i", which is zero, and add one to it.
These are equivalent because each of these elements in this list,
"the range of ten" the way we define it,
are just one more than the previous one.
So instead we could say "i" is going to be one more than it was before,
and so this should produce the exact same result.
Actually, if I don't want to get rid of this code,
if I want it there just for future reference, but I don't want
the Python interpreter to run it, I could comment them out,
and you comment things out by putting a little hash sign there.
So a hash sign there literally says,
"Hey, interpreter, ignore all of this stuff."
It could be useful if you have some code in there that you want to just not be interpreted for now,
and maybe you want to save it for later,
and it's also useful if you want to tell someone who's actually reading the code,
if you want to tell a human being whose reading the code,
what this code does.
So for example, you could write a little comment here that says,
"This while loop calculates the sum of zero through nine,"
and you'd say, "including nine."
So if someone were to come back later,
instead of having them figure out what this says,
they could read your comment and say,
"Okay, this part of the code right here calculates
the sum of zero to nine," and I could say, "and places it ..."
So when you put this hash tag,
it essentially comments out that line.
So if I want to go to another line I have put another hash tag there.
"... and places it in the variable 'sum'."
So then people know, "Woah, I know what it's doing,
and I know that when all is said and done,
the sum will be placed in this variable called 'sum'."
Anyway, let's run this program.
I want to show that it ignores these things.
If you wrote this text without a hash tag,
the computer, the interpreter, would say,
"What is all this? This isn't Python code,"
but this is useful for someone else reading,
and they'll also ignore this down here.
So let's...
...and then let's save it.
Let's save it and then let's run it.
It gives you the exact same results that we got before.
So these are the results that we got before (you might remember) :
zero, one, three, six, ten, all the way up to 45.
Now let's run this thing assuming that I haven't made any bugs ...
... and I get the exact same thing.
So this "while" loop is doing the exact same thing.
It's just doing a little bit ... it's more explicitly defining "i".
It takes you a little bit more code to write it,
but it's the exact same behavior.
nice and easy...thanks now i understand while loop :)
why can't we use range function in while loop?
I am absolutely new to high level coding, so in advance i apologize for asking too question, what i want to know is how does the for loop understands which is the last command of the loop (as there are no brackets like in c)
very easy to understand .... excellent for beginners
so good
it's really interesting to know about for loop. In python for loop is really different as compare to other language.
1
Can I always use for loop in place of While loop, everywhere, every time ?
THESE CLASSES ARE VERY DETAIL EXPLANATIONS I LIKE ALOT
So, I'm using 3.3.2, and I typed what you did, but it keeps saying "Invalid syntax". What do I do?