1
00:00:00,120 --> 00:00:03,850
So most of you probably know some programming language already.

2
00:00:04,560 --> 00:00:11,400
And in every programming, language loops are very important and also they are often combined with if

3
00:00:11,400 --> 00:00:20,460
statements so loops and if statements in Python are very similar to the cases in other programming languages.

4
00:00:21,180 --> 00:00:23,860
So let me show you here two examples for loops.

5
00:00:23,910 --> 00:00:25,380
The while loop and the for loop.

6
00:00:25,710 --> 00:00:28,710
And then we will also combine one of the loops with an if statement.

7
00:00:29,520 --> 00:00:31,530
So let me get started with a mile loop.

8
00:00:32,460 --> 00:00:38,940
So the syntax for a while loop is, of course, we start with while and then we have to define some

9
00:00:38,940 --> 00:00:39,450
condition.

10
00:00:40,170 --> 00:00:49,200
For example, we could say we have an index called I add this I should be smaller or equal to the number

11
00:00:49,200 --> 00:00:55,710
of 10, and then we add this to dots and this will start to loop.

12
00:00:56,880 --> 00:01:02,160
Now, when we press, enter the editor or the text editor here.

13
00:01:02,160 --> 00:01:06,180
Just you put a notebook in the browser already makes a tab here.

14
00:01:06,300 --> 00:01:08,880
And this is quite common for Python.

15
00:01:09,210 --> 00:01:14,400
So you do not write your commands here, but you make a tab so that it works fine.

16
00:01:15,570 --> 00:01:18,120
So here we have to define what will happen, actually.

17
00:01:18,690 --> 00:01:25,710
And since our condition will be that the number I must be smaller, equal to 10, we should do something

18
00:01:25,710 --> 00:01:26,160
with AI.

19
00:01:26,850 --> 00:01:31,140
First of all, we should define the number AI and we should do this outside of the loop.

20
00:01:31,150 --> 00:01:35,190
So we should say the starting role, if I could, for example, be one.

21
00:01:35,490 --> 00:01:42,450
And then in the loop, we increase the value of AI by one every time it loops.

22
00:01:42,570 --> 00:01:43,950
We increase the value by one.

23
00:01:44,730 --> 00:01:45,780
So I run this now.

24
00:01:46,200 --> 00:01:47,010
Nothing happens.

25
00:01:47,010 --> 00:01:53,000
But also there is no error because we start from April one and then we loop make it two, three, four

26
00:01:53,010 --> 00:01:54,180
five and so on.

27
00:01:54,180 --> 00:01:58,170
And then once we are at 11, the wire condition is no longer true.

28
00:01:58,650 --> 00:02:03,330
So the loop stops and the cell finishes the calculation.

29
00:02:04,770 --> 00:02:09,900
So what we can do instead is we can add another calculation here or another command.

30
00:02:10,350 --> 00:02:16,260
And I want to add it actually before I increase the index and I want to say it, just print the number

31
00:02:16,260 --> 00:02:16,620
I.

32
00:02:17,670 --> 00:02:24,150
So if I run this now, you see it loops 10 times, increase the value of AI and prints the value of

33
00:02:24,150 --> 00:02:24,560
AI.

34
00:02:24,570 --> 00:02:27,030
And then once I is 11, it stops.

35
00:02:27,540 --> 00:02:30,600
So this is a way to print the value.

36
00:02:31,200 --> 00:02:33,030
Of course, we could also do something like this.

37
00:02:33,630 --> 00:02:36,810
So then it gives us twice the value of ESD output.

38
00:02:38,910 --> 00:02:42,000
So what else can we do with while loops?

39
00:02:42,030 --> 00:02:49,110
Well, of course, we can add some if statements, and I want to show you here that I want to basically

40
00:02:49,110 --> 00:02:49,770
copy this.

41
00:02:50,430 --> 00:02:56,670
And what I want to do is I want to print the value of AI only when it's an even number and when it's

42
00:02:56,670 --> 00:02:57,330
an odd number.

43
00:02:57,330 --> 00:03:00,310
Instead, I want to print the word order.

44
00:03:01,350 --> 00:03:02,310
So how can we do this?

45
00:03:02,670 --> 00:03:04,160
Well, the beginning is quite good.

46
00:03:04,170 --> 00:03:10,140
We start from one, we increase the value and we stop ones is larger than ten.

47
00:03:10,770 --> 00:03:13,860
So the only thing that we have to change is this one here.

48
00:03:14,130 --> 00:03:20,100
And I do the following I write an if statement, which begins with if so, then here we have to write

49
00:03:20,100 --> 00:03:22,590
our condition, which I will do in a second.

50
00:03:23,280 --> 00:03:25,110
Then we will write this again.

51
00:03:25,110 --> 00:03:27,060
And you see it adds another tab.

52
00:03:27,660 --> 00:03:33,120
And here we write our command that we want to do or execute when the condition is true.

53
00:03:33,480 --> 00:03:44,220
So this would be print out and then we go one tap a back so that we are just at the level of if I will

54
00:03:44,220 --> 00:03:54,810
then write ours and if it's not true, then we we will print the words out.

55
00:03:56,280 --> 00:04:03,420
And then since this is once again at the same level as if and else this is outside of the if statement

56
00:04:03,420 --> 00:04:04,990
and this will be done every time.

57
00:04:06,120 --> 00:04:13,200
So the only thing that we have to now specify is the condition and the condition will be that AI is

58
00:04:13,200 --> 00:04:14,130
an even number.

59
00:04:14,610 --> 00:04:15,780
So how can we do this?

60
00:04:16,110 --> 00:04:20,430
Well, we can calculate the modulus, which I have explained to you at the very beginning.

61
00:04:20,700 --> 00:04:22,320
So we write like this.

62
00:04:22,830 --> 00:04:25,540
So this means the modulus of AI and two.

63
00:04:25,830 --> 00:04:31,050
So basically, the remainder of the divisions, I divide it by two and then you just take the remainder.

64
00:04:31,920 --> 00:04:37,470
If this is equal to zero, then it means it's an even number.

65
00:04:37,500 --> 00:04:43,230
For example, if I is eight, then eight divided by two is four with no remainder.

66
00:04:43,230 --> 00:04:44,190
So this is true.

67
00:04:44,370 --> 00:04:46,200
So it will print the value eight.

68
00:04:47,100 --> 00:04:52,440
If it's seven, then seven divided by two is three with a remainder of one.

69
00:04:52,800 --> 00:04:53,810
So this is false.

70
00:04:53,820 --> 00:04:55,830
So it goes to Alt's and prints OT.

71
00:04:57,060 --> 00:04:59,840
So if I run this, you see for every odd number.

72
00:05:00,120 --> 00:05:03,950
Just Prince, T-Bird, odd and even number it prints to value itself.

73
00:05:05,030 --> 00:05:10,010
So pretty easy, I think, and pretty much the same as in every other programming language.

74
00:05:11,120 --> 00:05:15,410
So the for loop is also, of course, pretty similar.

75
00:05:16,550 --> 00:05:24,260
So here's the only thing or the nice thing where it is useful could be to work with lists so you could

76
00:05:24,260 --> 00:05:30,560
write for and you have some value, some variables, some running index, for example.

77
00:05:30,560 --> 00:05:37,880
So we could say for AI in and then we can add here a list.

78
00:05:38,270 --> 00:05:40,310
So just some random list numbers.

79
00:05:41,930 --> 00:05:49,820
So this means that we have a running index AI, which takes every well, you off this list and then

80
00:05:49,820 --> 00:05:55,070
the first loop, it takes this value and the second loop it takes this value and so on.

81
00:05:55,550 --> 00:05:59,270
And then the fourth one, this one and then sends the list ends.

82
00:05:59,480 --> 00:06:00,680
Also, the loop ends.

83
00:06:01,700 --> 00:06:08,600
So this means now we can write what should happen with AI, given the fact that it changes the value

84
00:06:08,600 --> 00:06:10,460
every time it runs through the loop.

85
00:06:11,180 --> 00:06:15,230
And of course, the easy thing would be to just print the value of AI.

86
00:06:16,430 --> 00:06:19,280
So this would give us these four values, of course.

87
00:06:20,600 --> 00:06:26,630
And what I found pretty cool is that the same thing happens or works not only with lists, but also

88
00:06:26,630 --> 00:06:27,380
with strings.

89
00:06:28,340 --> 00:06:35,840
For example, I write for AI in the world, one in the string test print.

90
00:06:35,840 --> 00:06:39,230
I think you see the result will be all the letters.

91
00:06:39,830 --> 00:06:45,200
So basically, it treats the string like a list of characters.

92
00:06:45,650 --> 00:06:49,700
And so in the first loop, the AI will have to value T and so on and so on.

93
00:06:50,300 --> 00:06:51,200
So that's pretty cool.

94
00:06:53,030 --> 00:07:01,460
Now what I want to do next is I want to restore or redo this example of the while loop with the if statement,

95
00:07:01,460 --> 00:07:02,660
also with the for loop.

96
00:07:03,680 --> 00:07:08,900
So since it will be pretty similar, let me just copy this and now we have to modify it.

97
00:07:09,770 --> 00:07:11,540
And of course, we have to modify this one.

98
00:07:12,470 --> 00:07:15,920
I think the rest can be, yeah, can be as it is.

99
00:07:16,520 --> 00:07:27,860
So what we do is we ride for AI in and now we must make a list basically from one to and so on until

100
00:07:28,460 --> 00:07:28,850
10.

101
00:07:30,080 --> 00:07:32,150
And this is, of course, a bit tedious.

102
00:07:32,180 --> 00:07:34,280
So maybe let me start it.

103
00:07:34,280 --> 00:07:41,330
And for now, I mean, it's OK as only 10 numbers, so I can show you what happens

104
00:07:44,090 --> 00:07:44,930
for this one.

105
00:07:45,200 --> 00:07:46,310
So like this?

106
00:07:47,000 --> 00:07:50,540
So now we have the same output as before.

107
00:07:51,140 --> 00:07:52,550
So you see, it works.

108
00:07:53,240 --> 00:07:58,010
But a trick that I want to show you is, how can you optimize this long list here?

109
00:07:58,100 --> 00:08:03,020
Because if you would say, I want to do this for the first 1000 numbers, then it would be very tedious

110
00:08:03,020 --> 00:08:05,810
to write all of these numbers down in a nice way.

111
00:08:05,870 --> 00:08:08,990
To construct such a list is to range commands.

112
00:08:09,770 --> 00:08:11,750
So the range command works like this.

113
00:08:12,260 --> 00:08:19,730
You basically give a starting value and you give an end value and then it takes all the numbers and

114
00:08:19,730 --> 00:08:21,230
creates a list out of it.

115
00:08:22,040 --> 00:08:25,320
So if I run this, it gives me almost the correct result.

116
00:08:25,340 --> 00:08:28,280
It starts with one and ends with nine.

117
00:08:29,000 --> 00:08:32,900
So this is once again the same problem as we have encountered before.

118
00:08:32,900 --> 00:08:36,679
We have to take the ending index one number higher.

119
00:08:37,909 --> 00:08:43,799
So like this, by the way, you could also leave out the first number.

120
00:08:44,300 --> 00:08:47,670
But then the I would start at zero, which is not what we want.

121
00:08:47,690 --> 00:08:51,380
So we just tried one 11 for the range arguments.

122
00:08:52,700 --> 00:08:54,410
So I think that's pretty good.

123
00:08:54,620 --> 00:09:01,970
So we have now discussed the while loop and we have also discussed the for loop.

124
00:09:03,020 --> 00:09:12,260
And the only thing that is left that I want to show you is I want to show you how you can like very

125
00:09:12,260 --> 00:09:15,980
easily easily store these outputs in a list.

126
00:09:16,340 --> 00:09:23,990
And for this, you just write something like store storage or store list and make it an empty list,

127
00:09:24,170 --> 00:09:25,220
which would be this one.

128
00:09:26,000 --> 00:09:33,870
So just to square brackets which indicate that this one is a list which has no entries yet, and then,

129
00:09:33,890 --> 00:09:43,430
for example, you can just write for I in range of the numbers one, two six and what do you want to

130
00:09:43,430 --> 00:09:48,200
do is you want to now append the number to the list.

131
00:09:48,470 --> 00:09:55,220
So you write store list, dot append and then you write here what you want to append.

132
00:09:55,220 --> 00:09:58,580
And for example, I want to attend the square of these numbers.

133
00:09:59,820 --> 00:10:05,490
Are on this, and you see, now we have stored these numbers in the store list variable.

134
00:10:06,090 --> 00:10:09,990
So here you have square numbers one, four, nine, 16 and 25.

135
00:10:11,010 --> 00:10:15,870
And in the following, we will work further with lists and with loops.

136
00:10:16,200 --> 00:10:21,930
And I want to show you how you can store this information in files and how you can load the information

137
00:10:21,930 --> 00:10:23,100
back into your notebook.

