All language subtitles for 048 Using the for loop with Python Lists.en

af Afrikaans
ak Akan
sq Albanian
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

1

The first topic I want to talk about today is the concept of loops,

2

things that have to happen over and over and over again.

3

The first type of loop I want to introduce you to is something called the For

4

loop. And it can be used really easily in combination with something we learned

5

in yesterday's lessons, which is lists. By using a

6

for loop like this

7

we can go through each item in a list and perform some action with each

8

individual item.

9

So let's try it out in practice. In order to follow along

10

go ahead and fork the day 5 start rebel.

11

And once I've explained the concept, then I want you to try it out yourself.

12

But first, let's say we had a list called fruits and it contained three items,

13

apple, peach, and pear.

14

If we wanted to access each item in this list individually and print it out one

15

by one, then we would use a for loop.

16

So if we start out with the keyword for,

17

and then we give a name to a single item.

18

So in this case, we might call it fruit.

19

And then we use the in keyword

20

and finally the list that we want to loop through,

21

which is our fruits here.

22

Now we cap it off with a colon and we go on to the next line and notice how it's

23

indented there.

24

Now I'm going to go ahead and print the value of each of the fruit variables.

25

And if I run this code,

26

you'll see that it loops through my list of fruits and for each of the fruits

27

inside the list, it prints it out into the console,

28

apple, peach, pear, in that order. Now,

29

the important thing to realize here is that basically you can imagine that

30

behind the scenes, what this code is doing is

31

it's taking this list of fruits

32

and it's a signing a variable name, fruit, to each of them.

33

So the first time this runs fruit is equal to apple,

34

the second time this runs fruit is equal to peach,

35

and we can see this even more clearly

36

if you run it through the Thonny IDE,

37

which I told you about at the beginning of the course. Now,

38

if I go ahead and click on the debug icon and I step into each of the steps,

39

you can see first it establishes that we have a list of three strings,

40

apple, peach, pear, and then we go into the for loop.

41

Now notice on the right here I've got all the variables being accounted for.

42

And the first one it's noticed is the variable fruits,

43

which holds a list of strings. Now,

44

as I continue into the for loop,

45

notice how it's going to look through this list of fruits

46

and it's going to assign the variable name fruit to each of the items starting

47

from the first one apple.

48

So now by the time it's reached line 3,

49

we've already got this variable called fruit

50

that's been assigned to the value of apple. And so at this point,

51

printing out the value of this fruit

52

it's obviously going to print out apple and you'll see in the next step.

53

There we go. Now, once I'm done here,

54

then it's going to loop back to the start of the for loop

55

and now this variable fruit is going to be assigned to the next value inside the

56

list of fruits.

57

So now notice how the variable fruit is attached to the value peach,

58

and then it continues this and so on and so forth until it prints out each one

59

of the fruits.

60

And this really emphasizes the most important aspect of loops.

61

The loop allows us to execute the same line of code multiple times.

62

In this case, we're executing the print statement three times,

63

but our for loop isn't limited to just executing a single statement.

64

We don't just have to print out the name of an item in the list.

65

We can execute a whole block of statements multiple times,

66

and we can do many things inside this for loop and by inside, I mean indented.

67

So let's say that in addition to printing out the fruit,

68

I'm going to write another print statement and I'm going to not only print out

69

the name of the fruit,

70

but I'm also going to say fruit + + "Pie."

71

So when I run this code, what do you think will happen?

72

Well, it prints out apple, and then it prints out Apple Pie

73

And then it goes back to the start and it assigns the variable fruit to the next

74

item, peach peach pie, pear pear pie.

75

So this is how we can implement a simple for loop that loops through a list

76

and assigns a variable name to each of the items in the list in order,

77

and then inside the for loop after the colon, after some indentation,

78

we can do something with that temporary variable for each of the items.

79

Now I've been talking a lot about the concept of being inside a for loop versus

80

being outside a for loop. And this is really, really important.

81

Whenever you see a colon say in our if statement that we saw previously,

82

or the for loop that we're using here,

83

the indentation is really,

84

really important because if it's indented,

85

then it means that it's inside the for loop and these instructions

86

all get carried out for as many times as the for-loop will need to repeat.

87

Now, if I decided that I wanted to say print my,

88

uh, fruits,

89

so my list of fruits up here, and I put it inside my for loop,

90

so indented after the for loop, then as you can imagine,

91

it's going to print that for as many times as the loop runs,

92

which is going to be three because there's three items in our list. Now,

93

if I had indented that back to the beginning,

94

so now it's no longer inside the for loop,

95

then it's only going to print once and it's going to print it after the for

96

loop is done. So notice how it's doing the whole Apple, Apple pie,

97

et cetera. And then once the loop is finished,

98

then it jumps to the next line and carries out this instruction.

99

So the indentation is really, really important,

100

and you have to be really careful with this.

101

Have a play around with this code

102

and if you're still struggling to see how it works

103

then move the code over to Thonny and step through each step one at a time and

104

see how it works.

105

But I hope you're starting to see how loops are really handy at executing an

106

instruction over and over and over again,

107

getting the computer to save us time and energy.

108

A really good example of when loops would come in really handy is,

109

you know, how Bart Simpson gets punished

110

and it has to write out a sentence over and over again on the Blackboard. Well,

111

if only Bart was a programmer, then he would be able to use loops to do this

112

and he would be able to chill in the corner. Now,

113

once you're happy with this type of for-loop,

114

then head over to the next lesson where I've got a coding exercise

115

that's going to put your knowledge to the test. All right.

116

So I'll see you there.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.