In this section, you will explore data types, variables, input and output functions, and discover more about calculations and mathematical operators in Python.
Sarah: In the last video, you saw entering a few Python commands at the interactive prompt. In this one, we're going to write a very short program, setting up some variables and using them to do a calculation. And the problem we're going to solve is, you have collected rainfall measurements for a week, in millimeters, and you want to know the average. So, how do we do that in Python? So, we open the IDLE programming environment again, it comes up here with this interactive shell, which is fine for typing in commands and seeing them that you've made. If you want to write a program, we have to make a file with our commands in the file and it'll run through it. So, how we do that in this, is we do file, we do new file and here it's opened me a new file called untitled at the moment, where I'm going to write my Python code. We'd normally start with something explaining what we're doing because you can forget for a start, you can come back to a thing and completely forget, so it's always good to put information about what you're doing. In Python, a comment starts with a hash, and the Python interpreter, pays no attention to anything that starts with a hash, so you can write any, sort of, remarks to other programmers or to yourself that you want with a hash, and it won't bother Python. So, I'm going to do a comment for now, to start this off, and it's going to say, 'A program to find the average rainfall for a week when we collected measurements every day.' And this is a very simple problem and you could obviously do it with a hand calculator, but we're going to just see setting up some variables and running the code more as a program, rather than just things at the prompt. So, what shall we have? We'll have variables for all the measurements, and you remember I can call a variable whatever I like, and I can put whatever I like in a variable.
So, I'm going to say, Monday, I'll call Mon, equals 1.2, this is going to be in millimetres. Tuesday equals 3.3, Wednesday equals 1.1, Thursday equals 4.8, say, Friday equals nought, Saturday equals, say nought again, why not, and Sunday equals four. So, say those were the measurements we took over a week. What I've done is, I've made variables for each one separate, seven of them, and I've put the numbers in there. That's saying set the variable Mon to equals 1.2, set the variable Tuesday to 3.3, then I can do a calculation. We've seen, in the reading that we've got all the usual math, arithmetic operators in Python, so we can use plus, we can use minus, we can use, times is an asterisk, divide is that, a forward slash. We also have extra ones that we won't need for this, this program, we've got modulus, and we've got raising to the power, they all, they're all there to be used and you can use brackets as well, just as if you were doing ordinary school algebra. So, how we work out the average, I'm going to make a new variable called 'ave', and I do it by adding them all together and dividing by the number there are. So, I will say, Monday plus Tuesday, plus Wednesday, plus Thursday, plus Friday, plus Saturday, plus Sunday, that's all of them added together, it looks okay. I'm going to need to divide by seven, so I'll have to put in brackets to make sure I'm dividing the whole thing by seven, not just the last bit because there are seven, aren't there? Yes.
We often think when we're writing a program, we think what do we need? What's our input? What are we working on? What do we know?' What we have to do to it and what we want at the end. So, what we wanted at the end from this is, we want to know the average, so when I run this program I want it to tell me that, and how I do that in Python is I do print, and then I say-, I could just print ave. I shall do that for now, just print av, and this looks to me as if it's probably alright. I've added them all together, I've divided by seven and I'm printing it. So, often we find at this point it does an error, but we hope not. It'll ask you to save, if you haven't saved it yet, so I'm going to call this 'Rain.' I've had things called Rain before, so it's just warning me that I am replacing something. So, it did work, it produced me an answer of 2.0, and lots and lots of decimal places. The answer turns up in the shell that you're running with, so what I'm going to do to make it clearer is to put them into two sections, so that we can see them both at once. So, that's the program. This is not a very useful piece of output, it, it tells us that, hopefully the right answer, but it doesn't explain anything, it also has too many decimal places, so we can fix that. Print will let you do lots of things in it, so I'm going to say the average rainfall, that's just a string, a plain string not doing anything. It's going to print that as it is, then it's going to print my av. So, if I do run and see if that works alright. The average rainfall is, so, okay, I want a PS probably and I should say in millimeters, just to make it really good. So, that's that.
We also have this slight issue that it doesn't look very professional with all those decimal places, and we can use a built-in Python function called round, to round off our number. So, it can say, 'Round av,' and then you tell it how much you want rounded to, so two is fairly common for that. So, let's have a look what it does now and then you tap the closing bracket for the print, so here. So, run it again, and now it's making a bit more sense. It's saying the average rainfall in millimeters, which we've got in our information, is 2.06, rounded off, so it looks a little bit better. So, that's the first program, and we've, what we've done is, we've opened a file to store our program in. It's made a file called, 'rain.py', I chose the rain bit but Python programs have a .py extension, and when I tell it to run the program it runs through every line doing it one by one, sets these variables, works out the calculation and prints out whatever I've told it to print out in the print. We also used another built-in function, round, to make the output a little bit nicer. There's a lot of things we could improve about this, and we're going to learn to do some of them in the next video. For one thing, this program has its data built into the program and I can't use it, run it on new data, like next week's rainfall measurements, without actually editing the program. So, it would be nice to have something a bit more flexible than that. Okay, so we've seen writing a very simple program in Python.
A world-leading tech and digital skills organization, we help many of the world’s leading companies to build their tech and digital capabilities via our range of world-class training courses, reskilling bootcamps, work-based learning programs, and apprenticeships. We also create bespoke solutions, blending elements to meet specific client needs.