Lesson 2:
Crunching numbers
Year 8 – Intro to Python programming
Starter activity
True or false? .
This program will always produce the same output, whenever it is executed.
Make predictions (think, pair, share)
Question .
What will be the output of print when this program is executed?
lucky = 13
print("My lucky number is", lucky)
1
2
3
4
▹
True
Starter activity
Question .
What will be the output of print when this program is executed?
print("My lucky number is", lucky)
lucky = 13
1
2
3
4
Make predictions (think, pair, share)
▹
During program execution, a variable must have been assigned a value before that value
is referenced.
Starter activity
True or false? .
This program will always produce the same output, whenever it is executed.
Question .
What will be the output of print when this program is executed?
1
2
3
print("What’s your name?")
user = input()
print("Hello", user)
4
Make predictions (think, pair, share)
▹
False
Objectives
In this lesson, you will...
Activity 1
Assignments
days = 365
print(days, "days in a year")
This assignment does not mean that the days variable always equals 365.
This is an instruction to assign the value 365 to the days variable.
Assignments are not equations.
Assignments are instructions to be executed.
A subsequent assignment can assign a new value to the days variable, replacing the previous value.
Activity 1
days = 7 * 31 + 4 * 30 + 28
print(days, "days in a year")
Assignments with expressions
You can use expressions in assignments.
This is an instruction to evaluate the expression on the right
A subsequent assignment can assign a new value to the days variable, replacing the previous value.
Tip: Read assignments from right to left.
and then assign the value to the days variable on the left.
Activity 1
Arithmetic operators (in Python)
You can use these operators to form arithmetic expressions.
+ addition
- difference
* multiplication
/ division
Examples
a + 1 a plus 1
b - c b minus c
3 * d 3 times d
9 / 4 9 divided by 4 (value: 2.25)
// integer division
% remainder of integer division
** exponentiation
15 // 2 quotient of 15÷2 (value: 7)
15 % 2 remainder of 15÷2 (value: 1)
2 ** 8 2 to the power of 8 (value: 256)
Activity 1
days = 7 * 31 + 4 * 30 + 28
quad = 4 * days + 1
print(quad, "days in four years")
Referring to variables
An expression can refer to the values of variables.
During program execution, a variable must have been assigned a value before that value is referred to.
To evaluate this expression, the days variable must have been assigned a value.
Activity 1
days = 7 * 31 + 4 * 30 + 28
quad = 4 * days + 1
print(quad, "days in four years")
The machine
executes the code
days
365
Current instruction .
State .
Output .
Evaluate the expression
Calculate the days in a year.
?
and assign the value to days.
365
Activity 1
Evaluate the expression
The machine
executes the code
days
365
quad
1461
Current instruction .
State .
Output .
and assign the value to quad.
Calculate the days in four years.
?
days = 7 * 31 + 4 * 30 + 28
quad = 4 * days + 1
print(quad, "days in four years")
1461
Activity 1
days = 365
quad = 4 * days + 1
print(quad, "days in four years")
The machine
executes the code
days
365
1461 days in four years
quad
1461
Current instruction .
State .
Output .
Display the value of quad
and the literal "days in four years".
Display the result.
?
Activity 1
Order matters
You will be given a program that is supposed to convert a length of time from seconds to minutes.
Rearrange (change the order of) the statements, so that the program runs to completion without errors.
Use your worksheet.
Activity 2
Subtle points
number = 5
double = 2 * number
number = 15
A
Question .
What will be the value of double, after executing line ?
1
2
3
A
▹
Why .
Line only affects the number variable.
The value of double is not ‘updated’.
A
A
Activity 2
Subtle points
number = 5
number = number + 10
A
Question .
What will be the value of number, after executing line ?
1
2
3
A
▹
Why .
The expression number + 10 is evaluated
and the result is assigned to number.
The previous value of number is replaced.
A
4
Activity 3
Calculate age from year of birth
Use pair programming .
Driver
Control the keyboard and mouse.
Navigator
Provide support and instructions.
Alternate between roles.
Activity 3
Calculate age from year of birth
Create a program that asks the user for their birth year and calculates their age.
Live coding
print("Year of birth?")
birth_year = input()
age = 2020 - birth_year
print("You are", age, "years old")
age = 2020 - birth_year
TypeError: unsupported operand type(s) for -: 'int' and 'str'
Activity 3
print("Year of birth?")
birth_year = input()
age = 2020 - birth_year
print("You are", age, "years old")
what the call to input returns
"2008"
The input function always returns what the user typed as a string, i.e. a piece of text.
It is not possible to subtract a piece of text from a number, hence the error.
birth_year
"2008"
The text returned by input is assigned to the birth_year variable:
Calculate age from year of birth: commentary
Activity 3
print("Year of birth?")
birth_year = int(input())
age = 2020 - birth_year
print("You are", age, "years old")
This value is passed to the int function, which returns the corresponding integer.
what the call to input returns
"2008"
The input function always returns what the user typed as a string: a piece of text.
birth_year
2008
The expression is evaluated and the result is assigned to the age variable.
age
12
Calculate age from year of birth: commentary
Activity 4
How to input numbers
Work on programs that receive numerical input from the keyboard and process it.
Use your worksheets.
Activity 4
How to input numbers: solutions
print("Weight on Earth?")
weight_earth = int(input())
weight_moon = weight_earth / 6
print("Weight on moon:", weight_moon)
Activity 4
How to input numbers: solutions
print("How old are you?")
age = int(input())
dog_years = age * 7
print("You are", dog_years, "years old in dog years")
Homework
Answer the questions in your homework sheet.
Summary
In this lesson, you...
Next lesson, you will...
Use selection (if statements) to control the flow of program execution
Introduce elements of randomness into your programs
Used arithmetic expressions to calculate values
Used variables to store and reference values
Followed walk-throughs of code and kept track of variable values
Wrote programs that receive numerical input from the keyboard