In this topic students learn how to perform simple calculations using fixed numbers, numbers stored in variables and numbers input by the user.

Learning objectives and goals

Be able to read, comprehend, trace, adapt and create Python code that:

Slide deck

The slide deck for this topic is on Google Slides. A PDF copy is kept on Dodona too.

Teacher notes

This unit looks at how computers do basic maths. This is a process: it happens “inside” the computer.

We start off by learning about the operators used for simple maths. Multiplication and division use different operators to paper based maths. We also see the + operator used for another purpose here as well.

/ is used for division, but it returns a decimal result (known as a float). Python also lets us use // to perform floor (integer) division, where it ignores any decimal and just returns the whole number part of the answer (known as an integer, or int).

The tasks increase the complexity by replacing numbers in the code with variables, and then variables assigned using input. This requires casting.

Casting

Input in Python is always taken as strings (text), however calculations require the data to be in integer (whole number) or float (decimal number) form. To convert the data to the right format on input, casting is used.

Code examples

Addition:

5 + 4

Subtraction:

5 - 4

Multiplication:

5 * 4

Division:

5 / 4

Integer division, which only returns the whole number part of the answer:

5 // 4

Converts input to an integer:

variable = int(input())

Converts input to a float:

variable = float(input())

Errors and misconceptions to watch out for

Make sure that students check for the following things: