Loading...
Hi, I am Mr. Hogan.
We are going to have a great time today learning all about Python programming.
I'm so pleased you joined me today to learn all about it.
Shall we make a start? This is the second lesson from the "Introduction to Python Programming" unit.
It's called "Working with Numerical Inputs".
In this lesson, you will use arithmetic expressions to calculate values, and store these values in a variable in a program.
We've got four keywords for this lesson.
Variable, this is a named piece of data, stored in a memory location in a computer.
The data can change as needed.
Operator, this is a symbol, or word, that instructs the computer to perform a specific calculation or action.
String, a sequence of characters, which could be a combination of letters, numbers, spaces, or symbols.
Integer, a whole number that can be positive, negative, or zero.
We have three parts to today's lesson.
The first one is we are going to use variables to store values.
We're going to move on then to use arithmetic expressions to calculate values.
And then, at the end of the lesson, we will receive some input, and then convert it to a numerical value.
Let's start with the first part: use variables to store values.
Have you ever played a video game? In those video games, a variable is used to track your score.
It could be your score, it could be health points, or lives remaining.
A variable is held by the computer in a memory location.
Each variable is given a unique name, so you can easily find and use the data it holds.
The computer wouldn't know what value to present to you.
The data it holds can change, so, throughout your program, it could change the value that it holds.
You may have seen a Scratch block similar to this, where it sets the score to zero.
Let's have a quick check.
Which of the following is not a real-world example of something that could be represented by a variable? A, the temperature displayed on a weather app.
B, a countdown timer in a game.
Or C, the number of letters in the alphabet.
Remember you can pause the video at any time, and have a think about the answer.
Have you thought of the answer? It's C, the number of letters in the alphabet.
Well done for getting it right.
This program stores a value in a variable called score.
This is the variable score.
And this is the value 12.
This is known as an assignment statement.
It lets you store or update the value held by a variable in a program.
Assignment statements are not equations.
Assignments are instructions to be executed.
So we've got our score as a variable, and that is going to be assigned the value 10.
And then, we're going to use the print function to output score equals, and then the variable score, so that will be the value that is held in score, which is going to be 10.
You can see on the right hand side, we've got Scratch blocks that do a similar thing.
So this assignment stores the value 10 in a variable called score.
This value can be changed later in the program by using a similar assignment statement.
So all you do is change the value after the equals symbol.
So you could have score equals 12, or 15, or 5.
Let's have a quick check.
What sentence best describes assignments? Is it A, assignments are equations? B, assignments are instructions to be executed.
C, assignments are used to keep the value of the variable the same.
Have a think.
Obviously, you can pause the video at any time.
Have you decided on your answer? It's B, assignments are instructions to be executed.
Okay, let's have a quick practice.
Task A.
Consider this following Python code.
So we've got three lines of code with name as the variable, and then we have a print function on line three that prints the value which is going to be stored in name.
So the first question is which name will be printed? The second question is explain why this name is printed.
Give you some time to think, but you can also pause the video any time.
The third question is create variables to store the following pieces of information: name, favorite color, favorite food, and favorite animal.
You need to ask the user to input values for each variable.
Then, you need to output a summary of the person's favorite things using the print function.
Once you complete your program and run it, it should look a bit like this on the right-hand side.
You can pause the video any time to think about your answer.
Should we have a look at the answers? So the first question is which name will be printed? So the answer is Lucas.
The second question is to explain why this name, Lucas, is printed.
So the first variable on line one, it assigns the value Izzy, but then, on line two, it reassigns the name variable with Lucas, so, therefore, it's changed, and Lucas becomes the value stored in the variable.
This is a summary of the person's favorite things for question three.
This could be similar to your code.
So we can see from one, two, eight, lines one to eight, we can see that it's using the print function to ask what their name is, what their favorite color is, what their favorite food is, and what their favorite animal is, and then storing them in the relative variables that they've got there of name, color, food, and animal.
And then, from lines 10 to 13, we use the print function to display what value is held in those variables.
Okay, let's move on to the second part of the lesson.
We are going to use arithmetic expressions to calculate values.
An operator is a symbol, or special word, that instructs the computer to perform a specific calculation or action.
You may have seen these operators before in, say, a maths lesson.
And the simple operators we are going to use are: addition, subtraction, multiplication, and division.
An operator can be used in an expression to calculate another value.
So, for example, if we use the additional operator, and we have a variable of score, we could say score addition one, and the value of score will add one.
If we had minus one, the value of score will be subtracted by one.
Similarly, with multiplication, the value of score will be multiplied by two.
And division, the score divided by two.
Let's have a quick check.
In Python, which symbol is used to perform division? Is it A, a forward slash? Is it B, a backward slash? Is it C, a pipe? This sometimes looks like a capital I, or an L, but it's known as a pipe.
Or is it D, and obelus? Which you may have used when you're writing in maths.
Remember you can pause the video at any time.
Should we have a look at the answer? It's A, a forward slash.
Sofia has looked at this code, and is asking what value is assigned to each variable.
I think she's trying to work out what value is assigned to the variables on line three to six.
So what value is held in the variable add, the variable subtract, and the variable multiply, and the variable divide? And we can see from lines one and two that X is six, and Y is three.
So Aisha has got the answers.
So add is nine, because X is six, and it's adding Y, which is three.
Subtract is three, because, again, X is six, but, this time, it's subtracting Y, which is three.
So the answer to that is three, which is held in the variable subtract.
So, on line five, multiply is 18.
Can you see that? That's six multiplied by three.
And then, can you have a look on line six? Divide is actually two, because it is six divided by three.
Aisha is asking a very good question.
So, "Imagine an expression with lots of operations.
How does Python know which one to do first?" Sophia says, "It follows the order and priority of operations." Should we take a look at that? You are doing really well to get to this part of the lesson, so keep going and keep trying hard.
So this is the order of operations, or what you need to do first in an expression if it's got lots of different operators.
So we start from the top.
If there's anything in between brackets, we do that first, then we work out the indices, then do multiplication followed by division, and then addition followed by subtraction.
This assignment calculates the value in the arithmetic expression, and then stores it in the variable days.
So we can see that we're adding up all the days of the different months.
So there's seven months with 31 days in, there's four months with 30 days in, and there's one month with 28 days in.
Let's have a check now about your understanding of this.
What is the result of three add five, multiply two, subtract six, divide three? Is it going to be A, 10, B, seven, C, eight, or D, 11? Remember, you can pause the video at any time to have a think.
Should we take a look at the answer? It's D, 11.
And that's because we have to follow the order of operations.
So let's have a look at the expression.
We haven't got any brackets.
We haven't got any indices.
But we have got multiplication and division, and we carry these out left to right.
So the first thing we do is the multiplication of five multiplied by two, and that gives us 10.
Then we do the division, which is six divided by three, and that gives us two.
We do have addition and subtraction as well.
And, again, we do this from left to right.
So, now, we've got three plus 10 minus 2.
So we perform the addition first, which is 3 plus 10, which gives us 13, and then that gives us finally 13 minus 2, which gives us the answer 11.
It's great that you've understood everything to this part of the lesson.
An expression can use the value stored in variables.
To work out this expression, the days variable must have been assigned a value first.
And it has in line one.
So when running a program, a variable must have a value before that value is used in any expression.
Let's have a practice about this.
Look at this code.
It's been modified to include a leap year, which is 366 days instead of 365.
First question is what will the code output on line five? Two, the output is incorrect.
Explain one change that would make it output the correct value.
Remember, you can pause the video any time to work out the answer.
Three, write a Python program to calculate the total cost of apples.
You want to buy 10 apples, so you need to create the variable apples, and assign it the value 10.
Each apple costs 5p.
So you create the variable called apple_cost, and set the value to five.
Then, a discount coupon subtracts 3p off each apple.
Create a variable called discount, and set the value to three.
Calculate the total.
So calculate and store the total in a variable called total_cost.
Output the total cost.
So print the value of total_cost.
Remember, you can pause the video any time.
Take your time to create the code.
Okay, let's look at the answers.
So one, the code will print 1465 days in four years.
So 1,465 days in four years.
Let's have a look at the answer for question two.
1,465 days in four years is incorrect.
The program calculates the total number of days as if all four years were leap years.
The variable days is changed to 366 before the quad variable is calculated, making the calculation 4 multiplied by 366 first, add one, which equals 1,465.
Removing the line days equals 366 would calculate the total number of days in four years with one leap year.
So you've got 4 multiplied by 365 plus 1, which equals 1,461.
B, try and write a Python program to calculate the total cost of apples.
So this could be similar to what you've done.
So we've got the variables there, on lines one to three, with the different values assigned to the variables.
And then, we've got the expression which we've done, which we've then assigned to total_cost.
And then, we print the total_cost variable.
Let's move on to the third part of the lesson: receive input and convert it to a numerical value.
In programming, a data type defines the type of data a variable stores.
A string is the default data type in Python.
It holds sequences of characters like letters, numbers, spaces, and other symbols.
So here's a string that is assigned to the variable name.
An integer is a numerical data type which holds a whole number that can be positive, negative, or zero.
And we look at the assignment statements for these here.
So age equals int, which is short for integer, 12 in bracket, so it's gonna hold the integer 12 in age.
temp equals int(-2), so it's the negative number of two that has been assigned to the temp variable.
Let's have a quick check.
Which of the following data is not an integer? Is it A, 12? B, minus five? Or C, Lucas? Remember, you can pause at any time, and have a little think about the answer.
The answer is C, Lucas, because the others, A is a positive number, or integer, sorry, and B is a negative integer.
This program asks a user to input money they started with, then how much money they have spent.
It calculates the money they have left.
So, line one, we are asking the user how much money do they start with? And then, on line two, they input the answer to that string question.
On line three, we then ask, "How much did you spend?" And, again, on line four, it will then ask the user to input the answer.
Then, on line six, we've got a little expression which is assigned to the money_left variable.
And line seven, we've got a string with the variable within the string, which will print, "You have" whatever value is in money_left "left." However, when the program is run, it returns an error.
So it's a type error on line six.
Let's go back and have a look.
The input function always returns what the user typed as a string.
The value returned by this input is assigned to the starting_money variable.
It is not possible to subtract a string from another string, which is why we have this error.
To fix this error, the value is passed to the int function, which returns the corresponding integer.
So you can now see on line two, before the input function is used, we've got the int, and the same for line four.
So, now, when we work out money_left, it will actually do that calculation, because they are integers.
Let's have another check.
Which one of the following statements is true? Is it A, the input function does not return strings? B, the input function returns an integer, when the user types in numbers.
Or is it C, the input function returns a string, even when the user types in numbers? Remember to have a little think about it.
So you can pause the video at any time.
Let's look at the answer.
It's C, the input function returns a string, even when the user types in numbers.
Let's have a little practice.
Your science teacher asks you to make a program that reads the user's weight on Earth, and calculates how much the user will weigh on the Moon.
We know that gravity on the Moon is a sixth, one sixth, of what it is on Earth.
So, throughout this practice, you need to think about a program that will display a prompt and wait for input from the keyboard.
We need to think about the user types in a reply, so types a reply like 60.
And then, we want the program to display the results.
So one, open the starter file.
It's got some code in there already for you.
Two, complete line two, so that the program receives an input from the keyboard after displaying a prompt to the user.
Three, complete line three, so that the program calculates the weight on the Moon to be one sixth of the weight on Earth.
Remember, you can pause the video at any time to start looking at the code.
Four, a person's weight on the Moon is 16.
5% of what it is on Earth.
Complete line three, so that the program calculates the user's weight on the Moon according to this alternative description.
Five, extend your program to calculate the user's weight on other planets.
Remember, you can pause the video at any time.
Shall we have a look at the answers? So number one was just opening the starter code, and then two and three sort of go together.
So number two, complete line two, so the program receives input from the keyboard after displaying a prompt to the user.
So let's have a look at line two now in the code here.
So there we go.
We've got int(input()), making sure that when we receive input from the user after displaying the prompt on line one it is an integer.
And then, three says complete line three, so that the program calculates the weight on the Moon to be one sixth of the weight on Earth.
So let's have a look at line three.
Okay, so we've got the variable, now, weight_earth, which we assigned on line two, and we've divided it by six.
So that's correct.
Four, a person's weight on the Moon is 16.
5% of what it is on Earth.
Complete line three, so that the program calculates the user's weight on the Moon according to this alternative description.
So if we look on line three, we now have the expression with the variable weight_earth multiplied by 0.
165.
So in effect 16.
5%.
Number five was to extend your program, and to calculate the user's weight on any other planet.
So I wonder if any of you did that.
Well done, we've reached the end of this lesson.
In summary, we've learned that a variable is a name that represents a value stored in a computer's memory.
This value can be modified throughout a program.
We've also learned that there was an order of priority and operations when writing arithmetic expressions.
We've also learned that the input function in Python automatically returns a string.
To return an integer, the int function must be used.
Well done for getting to the end of this lesson.
I'm sure it was tricky along the way, but, hopefully, you came along with me, and we've learned all about arithmetic expressions in Python.