Welcome to today's bonus example.
In this session, we will be creating a function which gets some numbers from a text file and returns
the average of those numbers.
So please create an empty python file and in the same directory with your file.
Create a file directory.
So files and bonus 11 dot P are in the same directory inside files please create.
A text file.
So just a file with a name, dot, dot, text.
And there you can write temperatures.
And just write some dummy.
Values like that.
So that is our data file and we want to calculate the average of those numbers.
And this is our file text file.
So I'll have these data text file splits on the right so I can see both my codes and the data just for
convenience.
Let's start to write a function.
As you know, we start to define it with def keywords and give a name to the function.
Usually I like to name functions with a get, so that's when I call them.
Down here, I can write a variable which represents the returned value of that function.
So the name average makes sense.
And now this is like the process.
So I'll get the average from that process.
So the naming makes sense.
I think.
Right gets average.
First of all, we want to get the content of that file.
Here, you want to be careful with the file path.
So.
Bonus 11/8 in the same directory with files.
Therefore, we can say files here and then slash.
And then dot, dot, dot, txt.
We want to open that file in red mode as file.
And read the data with file that read lines.
So since we are interested to get all these as separate values, then we think about using read lines
instead of read.
Read would give us that entire texts as one single string, but this will give us that as a list of
strings.
So with that I can indent so that the file is open and then it's closed.
So at this point I have the data in here.
I don't need to work on the file anymore, so I indent, but I'm still indented under the function definition.
Everything is inside the function definition body.
So if I return the data at this point just to test things out and then I print the average average at
this point is just the list.
So let's check it out.
Run Bonus 11.
Yeah, that's what we get.
So it's good to take things step by step.
See the output.
And then try to improve it.
Especially when you are a beginner, you don't want to get overwhelmed by just writing a long list of
code lines.
Then you check the output and something doesn't work, and then it's hard to pinpoint the exact error.
So my suggestion is to create values as we did here.
Check it out if the value is correctly stored and so on.
So now we can go ahead and do the next step, which is to remove this item.
So that is the header of the text file.
Right.
And we want to calculate, calculate the average on the numbers.
But this is a problem.
So how do we remove that?
Well, you could apply here.
List slicing.
So that is a list, right?
That is the list.
Therefore, this syntax makes sense.
So that is the equivalent of.
Doing.
One, two, three.
Then you get for example, you get from one to the end of the list and that will give you only two and
three.
So the item with index zero, which is this item here, is excluded from the list, only the item with
index one which is declared in here.
So only the item with index one and everything after that is included in the slides.
We can do the same in here.
One like that rather the codes.
And yeah, now we get only the numbers.
But I would suggest you not to do that slicing in there or both.
You exit the file operations and just create a new variable.
So values is equal to data, and then you extract the values from the data and then return values here
and you get this the same output.
So there you have an intermediate variable.
It's better to have intermediate variables because it helps you debug your programs easier.
So you know that at this step you're simply reading the data.
Then at this step you are slicing them.
So that is better than doing everything in one line, reading and slicing better to have everything
separated.
Next.
You see that these are strings, so we need them as numbers.
How do you modify all the items of a list?
Try to think about that.
Well, using a list comprehension.
You say floats eye for eye in values.
So every item will be the float representation of itself for all the items in values.
So that is the existing list.
And we use the same variable to store the new lists in that existing variable.
So we update the value of that variable.
The new value will be the updated list, run the script and there we have a list of numbers.
I didn't use integer because these are continuous values, even though none of them is a decimal number.
I know that these are temperatures and with temperatures you theoretically you could have five points
one so you could have decimal numbers.
Therefore it makes sense to use a float data type to represent these values.
So now we have a list.
And next.
We calculate the average.
The average is the sum divided by the number of items.
So the sum of the numbers divided by the amount of numbers we have, we can get the sum using the sum
function.
It's a built in function and it gets a list as input.
So the list in our case is values.
And we divide the sum by the length of values.
In other words, this would be like we make the sum of them, which is 34, and then we divide that
by four.
That gives us the average, and then we turn the average here.
We do have this underline.
It says that this shadow's the name average from the outer scope.
So we can change these to average local.
But it's better to change it.
If you are on par charm, it is better to right click here, go to refactor, go to rename and say average
local and that will change all the occurrences of that variable in the code.
So you see that par charm knows that this is a local variable, so it will change the local variable
within the function.
It does not change that global variable there.
So press enter and there you have it.
So that's a local variable.
It's returned in here.
Then we get that return value.
We store it in this global variable and we print that out that value.
So run this.
And that's the average of those values.
So that's a neat function to get the values from a text file and give you the output.
Thanks for following this and I'll talk to you later.