Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Welcome back.
Up until now we've learned that with looping we can use four but there's actually a another way for
us to loop.
And it's called instead of a for loop a while loop and while loop is a little different in that we say
while a condition is happening do something.
So you can see that there's a bit of a difference here.
For example I can say that while zero is less than 50 do something or I can say for example I equals
to zero and I'm going to say while i is less than 50 print I now if I ran this program what do you think
will happen let's find out if I click Run
right.
Do you see that.
I don't know if you can tell but I'm scrolling right now through all of this.
It's looping looping looping looping looping and you see that the programming still running as a matter
of fact I have to stop it because what it was doing is it was looping and looping and looping because
if we look at the code well zero is always going to be less than 50.
So what happened was while this was true we printed AI and then the Python interpreter went back to
line 2 says hey is zero still less than 50.
Yeah.
OK.
Next line print is zero still less than 50.
Yeah ok next line print.
And it kept going keep going keep going.
Now this is what we call an infinite loop.
That is the program doesn't know when to stop.
I mean we're telling it to just keep going forever for eternity.
And I have some safeguards in place here.
But if you did this in code you'd get this infinite loop until your program crashes if you actually
ran this code.
Well it's going to keep running until the machine just doesn't have enough power anymore enough resources
to keep doing this over and over.
Everything has limits right.
So with a while loop we have to be really careful this infinite loop can be very dangerous.
So how can we solve this we had this idea of a break statement and break is.
Well just another key word in Python.
And as it as you may have guessed if I click Run look at that it only runs once as soon as Python sees
break it's going to say All right break out of the wild.
So it's kind of go to line five and it's going to say I got nothing there.
Program's done.
I'm going to exit out of the program but OK.
What if I want this to be looped 50 times.
How can we solve that issue.
Think about well we can just simply say I equals I plus 1 and then make sure that we change this too.
I now what's going to happen if I click Run we stop.
Well at forty nine we've done it 50 times we've lived through it 50 times because we kept incrementing
I over and over so that I each time we came back to the Loop was incremented by 1.
Now a shorthand for this we've seen it before is to do plus equals one.
So that if I run look at that same thing.
So we're also able to break out of the while loop as long as we're able to turn a condition into force.
So you'll see this a lot as well.
So to jump out of a while loop you can either turn the condition false or you can break out of the while
loop.
Now another interesting thing that we can do with a while loop is we can have else blocks.
And here we can say print done with all the work.
And if I click run here at the very end.
Look at that done with all the work.
And this should read like English right.
While I is less than 50 keep looping through here and as soon as we get to the last part where I becomes
50 where 50 is no longer less than 50 Python interpreter is going to say all right I don't care about
anything under there.
Let's go to the next line and I get otherwise.
So in this case we don't have anything else happening.
We can print done with all the work but you're probably asking yourself.
Hold on.
Can't I just not do this and just make sure my indentation is at the end click Run and that that works
right.
Why would I ever want to write else here.
What else is a special case here.
There is one thing that is useful.
That is if we added a for example a break statement here and I click run you'll see that to the else
statement does not print and that is because the else block will only execute if there isn't a break.
That is if the while condition turns to false then it's going to keep going and do the else.
But if there is a break it's going to break out of this whole thing and go to the eighth line.
So a little gotcha here that you won't see else too often next to while but there is that special case
where else is use when you want to make sure that this will be executed only if there isn't a break
statement.
Only if the loop is finished doing whatever it's doing without the breaks then let's take a break and
explore while loops a little bit more.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.