Lesson 2:
Crunching
numbers
Year 8 – Intro to Python programming
Starter activity
Make predictions (think, pair, share)
lucky = 13 Question .
print("My lucky number is", lucky) What will be the output of print when this
program is executed?
A.
1 My lucky number is lucky
▹ B. My lucky number is 13
2
3
C. It is not possible to know the output
without executing the program
D. There is an error in the program
4
True or false? .True
This program will always produce the same
output, whenever it is executed.
Starter activity
Make predictions (think, pair, share)
print("My lucky number is", lucky) Question .
lucky = 13 What will be the output of print when this
program is executed?
A.
1 My lucky number is lucky
B. My lucky number is 13
2
3
C. It is not possible to know the output
without executing the program
▹ D. There is an error in the program
4
During program execution, a variable must
have been assigned a value before that
value
is referenced.
Starter activity
Make predictions (think, pair, share)
print("What’s your name?") Question .
user = input() What will be the output of print when this
print("Hello", user) program is executed?
A.
1 Hello user
▹ B. Hello and whatever the user has typed
2
on the keyboard
C. It is not possible to know the output
3
without executing the program
D. There is an error in the program
4
True or false? .Fals
This program wille always produce the same
output, whenever it is executed.
Objectives
In this lesson, you will...
● Use arithmetic expressions to calculate values
● Use variables to store and reference values
● Follow walk-throughs of code and keep track of variable values
● Write programs that receive numerical input from the keyboard
Activity 1
Assignments
days = 365 Assignments are not equations.
print(days, "days in a year")
This assignment does not mean that the
days variable always equals 365.
Assignments are instructions to be
executed.
This is an instruction to assign the value
365 to the days variable.
A subsequent assignment can assign a
new value to the days variable, replacing
the previous value.
Activity 1
Assignments with expressions
days = 7 * 31 + 4 * 30 + 28 You can use expressions in assignments.
print(days, "days in a year")
This is an instruction to evaluate the
expression on and
thethen
rightassign the value to
the days variable on the left.
Tip: Read assignments from right to left.
A subsequent assignment can assign a new
value to the days variable, replacing the
previous value.
Activity 1
Arithmetic operators (in Python)
You can use these operators to Examples
form arithmetic expressions.
+ addition a+1 a plus 1
- difference b-c b minus c
* multiplication 3*d 3 times d
/ division 9/4 9 divided by 4 (value:
2.25)
// integer division 15 // 2 quotient of 15÷2
% remainder of integer (value: 7)
division 15 % 2 remainder of 15÷2
** exponentiation (value: 1)
2 ** 8 2 to the power of 8
(value: 256)
Activity 1
Referring to variables
days = 7 * 31 + 4 * 30 + 28 An expression can refer to the values of
quad = 4 * days + 1 variables.
print(quad, "days in four years")
To evaluate this expression, the days
variable must have been assigned a value.
During program execution, a variable must
have been assigned a value before that
value is referred to.
Activity 1
The machine
executes the code
days = 7 * 31 + 4 * 30 + 28 365 Current instruction .
quad = 4 * days + 1 Evaluate the expression
print(quad, "days in four years") and assign the value to days.
? Calculate the days in a year.
State .
days 365
Output .
Activity 1
The machine
executes the code
days = 7 * 31 + 4 * 30 + 28 Current instruction .
quad = 4 * days + 1 1461
Evaluate the expression
print(quad, "days in four years") and assign the value to quad.
? Calculate the days in four years.
State .
days 365
quad 1461
Output .
Activity 1
The machine
executes the code
days = 365 Current instruction .
quad = 4 * days + 1 Display the value of quad
print(quad, "days in four years") and the literal "days in four years".
? Display the result.
State .
days 365
quad 1461
Output .
1461 days in four years
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 Question .
double = 2 * number What will be the value of double, after
A number = 15 executing lineA ?
A.
▹ 1 10
B. 30
2
C. Line A is not a valid assignment:
3
number already has a value
Why .
Line A only affects the number variable.
The value of double is not ‘updated’.
Activity 2
Subtle points
number = 5 Question .
A number = number + 10 What will be the value of number, after
executing lineA ?
A.
1 5 and 15
▹ B. 15
2
C. There are no valid values for number
3
D. Line A is not a valid assignment:
4
number already has a value
Why .
The expression number + 10 is evaluated
and the result is assigned to number.
The previous value of number is replaced.
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
print("Year of birth?") Create a program that asks the
birth_year = input()
user for their birth year and
age = 2020 - birth_year
print("You are", age, "years old") calculates their age.
Live coding
age = 2020 - birth_year
TypeError: unsupported operand type(s) for -: 'int' and
'str'
Activity 3
Calculate age from year of birth: commentary
print("Year of birth?") The input function always returns what the
birth_year = input() user typed as a string, i.e. a piece of text.
age = 2020 - birth_year
print("You are", age, "years old") The text returned by input is assigned to
the birth_year variable:
It is not possible to subtract a piece of text
from a number, hence the error.
what the call
"2008"
to input
returns
birth_year "2008"
Activity 3
Calculate age from year of birth: commentary
print("Year of birth?") The input function always returns what the
birth_year = int(input()) user typed as a string: a piece of text.
age = 2020 - birth_year
print("You are", age, "years old") This value is passed to the int function,
which returns the corresponding integer.
The expression is evaluated and the result
is assigned to the age variable.
what the call
"2008"
to input
returns
birth_year 2008
age 12
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...
Used arithmetic expressions to Use selection (if statements) to
calculate values control the flow of program
execution
Used variables to store and
reference values Introduce elements of
randomness into your programs
Followed walk-throughs of code
and kept track of variable values
Wrote programs that receive
numerical input from the
keyboard