1
00:00:01,630 --> 00:00:10,200
In this lecture I'll explain one of the most important concepts in Python programming, that is for loops.

2
00:00:10,330 --> 00:00:16,600
I'll use an example, so you not only understand the syntax how to write a for loop, but you'll also understand

3
00:00:16,900 --> 00:00:18,790
why you should use for loops.

4
00:00:18,790 --> 00:00:26,890
Let's say we've got these temperatures for Monday so maybe this was recorded in the morning in midday,

5
00:00:27,490 --> 00:00:28,840
and that in the evening.

6
00:00:29,380 --> 00:00:37,640
So how can we make a program that prints out the rounded values of each of these values like that?

7
00:00:37,660 --> 00:00:39,430
So that would be the expected output.

8
00:00:39,640 --> 00:00:41,140
You can see it down here.

9
00:00:41,340 --> 00:00:48,010
As you'd get 9 because 9.1 rounded up to the whole number will be 9, and then

10
00:00:48,070 --> 00:00:49,620
8.8, 9 again.

11
00:00:49,630 --> 00:00:51,950
And  7.8, 8.

12
00:00:51,970 --> 00:00:53,440
How can we do that?

13
00:00:53,470 --> 00:01:00,310
So we need to find a way to access the items of the list and you know a method already how to

14
00:01:00,310 --> 00:01:04,750
access list items which is using indexes.

15
00:01:05,950 --> 00:01:09,370
So if you want the first one you use zero.

16
00:01:09,970 --> 00:01:19,260
So you want to print out the first item, that however would print out 9.1,  you want the rounded

17
00:01:19,270 --> 00:01:27,520
version of nine points one which is maybe you want to look, to have a look at what functions you have

18
00:01:27,880 --> 00:01:28,540
available.

19
00:01:32,990 --> 00:01:33,960
Like that.

20
00:01:33,990 --> 00:01:42,290
So have a quick look here, and you'll be able to find a round.
So round which means you want to round

21
00:01:42,350 --> 00:01:51,680
a number, so round 9.1, you get 9, and in our case the number we'd like to round up is this

22
00:01:51,680 --> 00:01:52,400
expression.

23
00:01:52,400 --> 00:02:02,120
So we want to play round here and in parentheses put that expression.

24
00:02:02,120 --> 00:02:05,330
So that's number, 9.1.

25
00:02:05,330 --> 00:02:10,650
So give it a try before rewriting the entire code and that's fine.

26
00:02:10,700 --> 00:02:12,480
Do that again.

27
00:02:12,490 --> 00:02:13,410
Write.

28
00:02:14,060 --> 00:02:21,830
But with 1 and with 2, executes, and you get the expected output, okay, that's fine, it works but what

29
00:02:21,830 --> 00:02:28,630
if you had a big list of 100 items?
You'd end up with 100 lines of code.

30
00:02:28,640 --> 00:02:32,920
Well, I can show you how to do that with just two lines of code instead.

31
00:02:33,370 --> 00:02:37,630
This how you do it. You use a for loop for temperature.

32
00:02:37,650 --> 00:02:46,040
Just choose a name for a variable for temperature in.
You see how these keywords are highlighted.

33
00:02:46,040 --> 00:02:49,340
So these are actually Python reserved words.

34
00:02:49,490 --> 00:02:50,920
Temperature is just a variable.

35
00:02:50,930 --> 00:02:56,180
That's why it's in black. In Monday temperatures, use a colon.

36
00:02:56,210 --> 00:02:57,550
And what happens after the colon?

37
00:02:57,560 --> 00:02:59,380
I told you in the earlier video.

38
00:02:59,660 --> 00:03:05,760
Well indentation which happens automatically in Visual Studio Code, four spaces.

39
00:03:05,870 --> 00:03:09,290
What do we want to do for each of the temperatures?

40
00:03:09,290 --> 00:03:17,930
Well, we want to print out the round form of temperature.

41
00:03:17,930 --> 00:03:21,310
Just like that, save, execute.

42
00:03:21,390 --> 00:03:22,820
And this is the output.

43
00:03:23,420 --> 00:03:33,680
So in just two lines of code we are able to execute some commands for all the items of an array which

44
00:03:33,680 --> 00:03:37,040
a least in this case, but it can also be a string.

45
00:03:37,190 --> 00:03:44,600
For example for the letter in "hello", print letter.

46
00:03:48,600 --> 00:03:51,280
And hello is printed out there.

47
00:03:52,230 --> 00:03:55,270
And of course you can do whatever you like there, title.

48
00:03:55,320 --> 00:03:59,620
Get the first item which is the letter.

49
00:03:59,640 --> 00:04:03,400
So the letter would be capitalized like that.

50
00:04:03,420 --> 00:04:09,250
So let me go one more time through these two lines to explain them what they mean.

51
00:04:09,360 --> 00:04:13,790
So what the loop does is for temperature in Monday temperatures.

52
00:04:13,830 --> 00:04:15,650
So you are creating a variable.

53
00:04:15,810 --> 00:04:23,580
And what the loop does is it goes through all these items one by one with loops.

54
00:04:23,580 --> 00:04:27,900
So in the first iteration temperature will be equal,

55
00:04:27,900 --> 00:04:31,960
so the variable will be equal to 9.1.

56
00:04:32,100 --> 00:04:39,420
Therefore the round function will round up nine point one and print it out here.

57
00:04:40,230 --> 00:04:45,900
And if there are more lies in here, for example print "Done".

58
00:04:46,650 --> 00:04:54,320
If you do that you see if there is another more line like after 9, this line is executed.

59
00:04:54,380 --> 00:04:56,790
So you get the string "Done" print it out.

60
00:04:56,870 --> 00:05:00,320
If there are more lines those lines will be executed.

61
00:05:00,320 --> 00:05:06,890
If there is no more lines, then the execution goes again on the top of the for loop, and the loop gets the

62
00:05:06,890 --> 00:05:08,120
second item this time.

63
00:05:08,480 --> 00:05:14,890
So temperature this time will be equal to 8.8 and the same thing happens again, rounded it up,

64
00:05:14,900 --> 00:05:16,040
print it out.

65
00:05:16,050 --> 00:05:19,200
"Done" is also printed out, goes to the next line.

66
00:05:19,430 --> 00:05:20,290
7.6.

67
00:05:20,300 --> 00:05:29,460
And so one, and then after 7.6  the loop tries to see if there are more items in this array.

68
00:05:29,720 --> 00:05:37,730
If there are no more items what happens is that this indented block, the execution of this block finishes

69
00:05:37,820 --> 00:05:43,220
and Python goes to the next two lines to execute what's left in the script.

