1
00:00:01,330 --> 00:00:03,650
Welcome to today's bonus example.

2
00:00:03,670 --> 00:00:10,900
In this session, we will be creating a function which gets some numbers from a text file and returns

3
00:00:10,900 --> 00:00:12,790
the average of those numbers.

4
00:00:14,170 --> 00:00:19,780
So please create an empty python file and in the same directory with your file.

5
00:00:21,040 --> 00:00:24,160
Create a file directory.

6
00:00:25,360 --> 00:00:32,650
So files and bonus 11 dot P are in the same directory inside files please create.

7
00:00:33,620 --> 00:00:35,570
A text file.

8
00:00:35,570 --> 00:00:39,390
So just a file with a name, dot, dot, text.

9
00:00:40,280 --> 00:00:43,040
And there you can write temperatures.

10
00:00:45,600 --> 00:00:47,220
And just write some dummy.

11
00:00:48,560 --> 00:00:50,420
Values like that.

12
00:00:50,930 --> 00:00:57,650
So that is our data file and we want to calculate the average of those numbers.

13
00:00:59,120 --> 00:01:01,580
And this is our file text file.

14
00:01:01,580 --> 00:01:09,560
So I'll have these data text file splits on the right so I can see both my codes and the data just for

15
00:01:09,560 --> 00:01:10,460
convenience.

16
00:01:12,620 --> 00:01:14,200
Let's start to write a function.

17
00:01:14,200 --> 00:01:19,720
As you know, we start to define it with def keywords and give a name to the function.

18
00:01:22,330 --> 00:01:28,330
Usually I like to name functions with a get, so that's when I call them.

19
00:01:31,070 --> 00:01:39,350
Down here, I can write a variable which represents the returned value of that function.

20
00:01:39,350 --> 00:01:41,510
So the name average makes sense.

21
00:01:41,900 --> 00:01:44,240
And now this is like the process.

22
00:01:44,240 --> 00:01:49,820
So I'll get the average from that process.

23
00:01:49,820 --> 00:01:52,010
So the naming makes sense.

24
00:01:52,010 --> 00:01:52,700
I think.

25
00:01:54,380 --> 00:01:55,880
Right gets average.

26
00:01:57,450 --> 00:02:02,040
First of all, we want to get the content of that file.

27
00:02:02,550 --> 00:02:05,340
Here, you want to be careful with the file path.

28
00:02:05,970 --> 00:02:07,170
So.

29
00:02:07,980 --> 00:02:11,009
Bonus 11/8 in the same directory with files.

30
00:02:11,039 --> 00:02:14,880
Therefore, we can say files here and then slash.

31
00:02:16,030 --> 00:02:19,420
And then dot, dot, dot, txt.

32
00:02:20,440 --> 00:02:25,210
We want to open that file in red mode as file.

33
00:02:29,330 --> 00:02:34,160
And read the data with file that read lines.

34
00:02:34,190 --> 00:02:42,440
So since we are interested to get all these as separate values, then we think about using read lines

35
00:02:42,590 --> 00:02:45,110
instead of read.

36
00:02:45,950 --> 00:02:55,550
Read would give us that entire texts as one single string, but this will give us that as a list of

37
00:02:55,790 --> 00:02:56,810
strings.

38
00:03:00,740 --> 00:03:08,390
So with that I can indent so that the file is open and then it's closed.

39
00:03:08,390 --> 00:03:10,760
So at this point I have the data in here.

40
00:03:10,760 --> 00:03:17,450
I don't need to work on the file anymore, so I indent, but I'm still indented under the function definition.

41
00:03:17,450 --> 00:03:21,260
Everything is inside the function definition body.

42
00:03:22,430 --> 00:03:30,440
So if I return the data at this point just to test things out and then I print the average average at

43
00:03:30,440 --> 00:03:31,970
this point is just the list.

44
00:03:31,970 --> 00:03:33,380
So let's check it out.

45
00:03:33,650 --> 00:03:35,120
Run Bonus 11.

46
00:03:35,330 --> 00:03:36,770
Yeah, that's what we get.

47
00:03:38,300 --> 00:03:40,200
So it's good to take things step by step.

48
00:03:40,220 --> 00:03:41,300
See the output.

49
00:03:42,900 --> 00:03:44,610
And then try to improve it.

50
00:03:44,640 --> 00:03:52,080
Especially when you are a beginner, you don't want to get overwhelmed by just writing a long list of

51
00:03:52,080 --> 00:03:53,160
code lines.

52
00:03:53,310 --> 00:04:00,180
Then you check the output and something doesn't work, and then it's hard to pinpoint the exact error.

53
00:04:00,360 --> 00:04:06,810
So my suggestion is to create values as we did here.

54
00:04:06,930 --> 00:04:13,110
Check it out if the value is correctly stored and so on.

55
00:04:13,830 --> 00:04:23,600
So now we can go ahead and do the next step, which is to remove this item.

56
00:04:23,610 --> 00:04:25,770
So that is the header of the text file.

57
00:04:25,770 --> 00:04:26,370
Right.

58
00:04:26,580 --> 00:04:31,070
And we want to calculate, calculate the average on the numbers.

59
00:04:31,080 --> 00:04:32,490
But this is a problem.

60
00:04:32,490 --> 00:04:34,500
So how do we remove that?

61
00:04:36,060 --> 00:04:41,250
Well, you could apply here.

62
00:04:41,280 --> 00:04:43,230
List slicing.

63
00:04:43,950 --> 00:04:45,990
So that is a list, right?

64
00:04:46,020 --> 00:04:47,030
That is the list.

65
00:04:47,040 --> 00:04:49,920
Therefore, this syntax makes sense.

66
00:04:49,920 --> 00:04:52,200
So that is the equivalent of.

67
00:04:54,360 --> 00:04:54,990
Doing.

68
00:04:54,990 --> 00:04:56,550
One, two, three.

69
00:04:57,180 --> 00:05:04,740
Then you get for example, you get from one to the end of the list and that will give you only two and

70
00:05:04,740 --> 00:05:05,310
three.

71
00:05:05,880 --> 00:05:13,560
So the item with index zero, which is this item here, is excluded from the list, only the item with

72
00:05:13,560 --> 00:05:16,770
index one which is declared in here.

73
00:05:17,070 --> 00:05:21,570
So only the item with index one and everything after that is included in the slides.

74
00:05:21,810 --> 00:05:23,490
We can do the same in here.

75
00:05:23,490 --> 00:05:26,490
One like that rather the codes.

76
00:05:26,490 --> 00:05:29,760
And yeah, now we get only the numbers.

77
00:05:31,200 --> 00:05:36,780
But I would suggest you not to do that slicing in there or both.

78
00:05:36,810 --> 00:05:43,410
You exit the file operations and just create a new variable.

79
00:05:43,410 --> 00:05:52,500
So values is equal to data, and then you extract the values from the data and then return values here

80
00:05:52,500 --> 00:05:54,220
and you get this the same output.

81
00:05:54,660 --> 00:05:57,120
So there you have an intermediate variable.

82
00:05:57,450 --> 00:06:04,500
It's better to have intermediate variables because it helps you debug your programs easier.

83
00:06:06,030 --> 00:06:11,640
So you know that at this step you're simply reading the data.

84
00:06:11,970 --> 00:06:14,580
Then at this step you are slicing them.

85
00:06:15,450 --> 00:06:22,410
So that is better than doing everything in one line, reading and slicing better to have everything

86
00:06:22,410 --> 00:06:23,310
separated.

87
00:06:25,360 --> 00:06:25,990
Next.

88
00:06:25,990 --> 00:06:29,640
You see that these are strings, so we need them as numbers.

89
00:06:29,650 --> 00:06:33,580
How do you modify all the items of a list?

90
00:06:34,810 --> 00:06:36,280
Try to think about that.

91
00:06:39,530 --> 00:06:42,140
Well, using a list comprehension.

92
00:06:44,050 --> 00:06:48,220
You say floats eye for eye in values.

93
00:06:49,310 --> 00:06:56,540
So every item will be the float representation of itself for all the items in values.

94
00:06:56,540 --> 00:06:58,400
So that is the existing list.

95
00:06:58,400 --> 00:07:04,220
And we use the same variable to store the new lists in that existing variable.

96
00:07:04,220 --> 00:07:07,280
So we update the value of that variable.

97
00:07:08,000 --> 00:07:16,040
The new value will be the updated list, run the script and there we have a list of numbers.

98
00:07:16,490 --> 00:07:26,300
I didn't use integer because these are continuous values, even though none of them is a decimal number.

99
00:07:26,300 --> 00:07:34,100
I know that these are temperatures and with temperatures you theoretically you could have five points

100
00:07:34,100 --> 00:07:38,150
one so you could have decimal numbers.

101
00:07:38,690 --> 00:07:44,000
Therefore it makes sense to use a float data type to represent these values.

102
00:07:46,180 --> 00:07:47,560
So now we have a list.

103
00:07:47,560 --> 00:07:48,760
And next.

104
00:07:53,950 --> 00:07:56,650
We calculate the average.

105
00:07:56,650 --> 00:08:02,050
The average is the sum divided by the number of items.

106
00:08:02,060 --> 00:08:08,020
So the sum of the numbers divided by the amount of numbers we have, we can get the sum using the sum

107
00:08:08,020 --> 00:08:08,560
function.

108
00:08:08,560 --> 00:08:13,840
It's a built in function and it gets a list as input.

109
00:08:14,080 --> 00:08:16,330
So the list in our case is values.

110
00:08:17,900 --> 00:08:23,540
And we divide the sum by the length of values.

111
00:08:24,170 --> 00:08:31,880
In other words, this would be like we make the sum of them, which is 34, and then we divide that

112
00:08:31,880 --> 00:08:32,929
by four.

113
00:08:32,960 --> 00:08:37,120
That gives us the average, and then we turn the average here.

114
00:08:39,490 --> 00:08:40,960
We do have this underline.

115
00:08:40,960 --> 00:08:44,950
It says that this shadow's the name average from the outer scope.

116
00:08:46,000 --> 00:08:49,300
So we can change these to average local.

117
00:08:49,900 --> 00:08:52,390
But it's better to change it.

118
00:08:52,390 --> 00:09:00,010
If you are on par charm, it is better to right click here, go to refactor, go to rename and say average

119
00:09:00,280 --> 00:09:04,960
local and that will change all the occurrences of that variable in the code.

120
00:09:05,800 --> 00:09:11,830
So you see that par charm knows that this is a local variable, so it will change the local variable

121
00:09:11,830 --> 00:09:12,910
within the function.

122
00:09:12,910 --> 00:09:15,850
It does not change that global variable there.

123
00:09:16,330 --> 00:09:19,480
So press enter and there you have it.

124
00:09:19,480 --> 00:09:20,890
So that's a local variable.

125
00:09:20,890 --> 00:09:22,210
It's returned in here.

126
00:09:22,240 --> 00:09:25,750
Then we get that return value.

127
00:09:26,710 --> 00:09:31,620
We store it in this global variable and we print that out that value.

128
00:09:31,630 --> 00:09:32,740
So run this.

129
00:09:32,740 --> 00:09:37,120
And that's the average of those values.

130
00:09:38,260 --> 00:09:47,830
So that's a neat function to get the values from a text file and give you the output.

131
00:09:48,850 --> 00:09:50,920
Thanks for following this and I'll talk to you later.

