Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
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
0 In this lecture I'll explain one of the most important concepts in Python programming, that is for loops. 1
I'll use an example, so you not only understand the syntax how to write a for loop, but you'll also understand 2
why you should use for loops. 3
Let's say we've got these temperatures for Monday so maybe this was recorded in the morning in midday, 4
and that in the evening. 5
So how can we make a program that prints out the rounded values of each of these values like that? 6
So that would be the expected output. 7
You can see it down here. 8
As you'd get 9 because 9.1 rounded up to the whole number will be 9, and then 9
8.8, 9 again. 10
And 7.8, 8. 11
How can we do that? 12
So we need to find a way to access the items of the list and you know a method already how to 13
access list items which is using indexes. 14
So if you want the first one you use zero. 15
So you want to print out the first item, that however would print out 9.1, you want the rounded 16
version of nine points one which is maybe you want to look, to have a look at what functions you have 17
available. 18
Like that. 19
So have a quick look here, and you'll be able to find a round. So round which means you want to round 20
a number, so round 9.1, you get 9, and in our case the number we'd like to round up is this 21
expression. 22
So we want to play round here and in parentheses put that expression. 23
So that's number, 9.1. 24
So give it a try before rewriting the entire code and that's fine. 25
Do that again. 26
Write. 27
But with 1 and with 2, executes, and you get the expected output, okay, that's fine, it works but what 28
if you had a big list of 100 items? You'd end up with 100 lines of code. 29
Well, I can show you how to do that with just two lines of code instead. 30
This how you do it. You use a for loop for temperature. 31
Just choose a name for a variable for temperature in. You see how these keywords are highlighted. 32
So these are actually Python reserved words. 33
Temperature is just a variable. 34
That's why it's in black. In Monday temperatures, use a colon. 35
And what happens after the colon? 36
I told you in the earlier video. 37
Well indentation which happens automatically in Visual Studio Code, four spaces. 38
What do we want to do for each of the temperatures? 39
Well, we want to print out the round form of temperature. 40
Just like that, save, execute. 41
And this is the output. 42
So in just two lines of code we are able to execute some commands for all the items of an array which 43
a least in this case, but it can also be a string. 44
For example for the letter in "hello", print letter. 45
And hello is printed out there. 46
And of course you can do whatever you like there, title. 47
Get the first item which is the letter. 48
So the letter would be capitalized like that. 49
So let me go one more time through these two lines to explain them what they mean. 50
So what the loop does is for temperature in Monday temperatures. 51
So you are creating a variable. 52
And what the loop does is it goes through all these items one by one with loops. 53
So in the first iteration temperature will be equal, 54
so the variable will be equal to 9.1. 55
Therefore the round function will round up nine point one and print it out here. 56
And if there are more lies in here, for example print "Done". 57
If you do that you see if there is another more line like after 9, this line is executed. 58
So you get the string "Done" print it out. 59
If there are more lines those lines will be executed. 60
If there is no more lines, then the execution goes again on the top of the for loop, and the loop gets the 61
second item this time. 62
So temperature this time will be equal to 8.8 and the same thing happens again, rounded it up, 63
print it out. 64
"Done" is also printed out, goes to the next line. 65
7.6. 66
And so one, and then after 7.6 the loop tries to see if there are more items in this array. 67
If there are no more items what happens is that this indented block, the execution of this block finishes 68
and Python goes to the next two lines to execute what's left in the script.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.