1 of 24

Lesson 2:

Crunching numbers

Year 8 – Intro to Python programming

2 of 24

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?

  1. My lucky number is lucky
  2. My lucky number is 13
  3. It is not possible to know the output without executing the program
  4. There is an error in the program

lucky = 13

print("My lucky number is", lucky)

1

2

3

4

True

3 of 24

Starter activity

Question .

What will be the output of print when this program is executed?

  1. My lucky number is lucky
  2. My lucky number is 13
  3. It is not possible to know the output without executing the program
  4. There is an error in the program

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.

4 of 24

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. Hello user
  2. Hello and whatever the user has typed on the keyboard
  3. It is not possible to know the output without executing the program
  4. There is an error in the program

1

2

3

print("What’s your name?")

user = input()

print("Hello", user)

4

Make predictions (think, pair, share)

False

5 of 24

Objectives

  • 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

In this lesson, you will...

6 of 24

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.

7 of 24

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.

8 of 24

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)

9 of 24

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.

10 of 24

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

11 of 24

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

12 of 24

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.

?

13 of 24

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.

14 of 24

Activity 2

Subtle points

number = 5

double = 2 * number

number = 15

A

Question .

What will be the value of double, after executing line ?

  1. 10
  2. 30
  3. Line is not a valid assignment: number already has a value

1

2

3

A

Why .

Line only affects the number variable.

The value of double is not ‘updated’.

A

A

15 of 24

Activity 2

Subtle points

number = 5

number = number + 10

A

Question .

What will be the value of number, after executing line ?

  1. 5 and 15
  2. 15
  3. There are no valid values for number
  4. Line is not a valid assignment: number already has a value

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

16 of 24

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.

17 of 24

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'

18 of 24

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

19 of 24

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

20 of 24

Activity 4

How to input numbers

Work on programs that receive numerical input from the keyboard and process it.

Use your worksheets.

21 of 24

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)

22 of 24

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")

23 of 24

Homework

Answer the questions in your homework sheet.

24 of 24

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