CS08 : Mathematical Methods

Time to learn about the different mathematical methods that we can use during our programming. Some you will recognise, some you wont.

https://docs.google.com/presentation/d/12bI670tIvI72VZxNh0rNPpv59rUWAkutA6m6jqNhA6M/preview?slide=id.g3cb6bc7fc1_0_0Learning Outcomes
Mathematical Methods

We are learning ...
  • How to use computers to perform mathematical operations
So that we can ...
  • Have practical experience using a program language to perform arithmetic operations
    - Addition
    - Subtraction
    - Multiplication
    - Real division (float division)
    - Integer division (DIV)
    - Remainder division (MOD)

# Get Ready.png

Opportunities for paired programming

https://docs.google.com/presentation/d/12bI670tIvI72VZxNh0rNPpv59rUWAkutA6m6jqNhA6M/preview?slide=id.g3df8b56503_0_5Activity 1
What do these signs mean? 

Computer are really good at maths. They do maths like you do maths, except that they do it a lot faster (currently peaking at 93 quadrillion mathematical operations per second). However, fundamentally, they are the same. But can you do maths as fast as Scott Flansburg?

DIRECTV MATHx Event 2015 - Scott Flansburg

Task 1.1 Spot the operator
Where we learn that mathematical operators in programming look different

Do you recognise these?

Look at the following list of mathematical operators. 

Exponentiation : ny
Multiplication : ×
Division       : ÷
Addition       : +
Subtraction    : -

Look for the multiplication symbol, the division symbol and some way of expressing exponentiation on your computer keyboard. Can you find them? 

Different symbols

Different symbols are used for multiplication, division and exponentiation in programming ...

Exponentiation : **
Multiplication : *
Division       : /

In your notebook / on paper

Write down the complete list of programming mathematical operators (in blue) and explain what function each of them performs.

Some things are!

https://docs.google.com/presentation/d/12bI670tIvI72VZxNh0rNPpv59rUWAkutA6m6jqNhA6M/preview?slide=id.g41ede566e7_0_11Activity 2
Using Python as a calculator

All programming languages allow you to carry out mathematical operations. Scripted languages like Python also have a console where you can evaluate mathematical expressions. Often, the results of these expressions are assigned to variables so that they can be used later.

Task 2.1 Using Python as a calculator
Where we learn how to use Python as a simple calculator

Try these first (with your brain)

Answer the following questions, jotting down your ideas in your folders.
  1. If widgets = 12 + 4, what is widgets?
  2. If fronds = 14 × (3 + 44), what is fronds?
  3. If gratings = 9 and lines = 5, what is zones if zones = gratings × lines?
  4. If base = 12 and height = 3, what is area if area = 0.5 × base × height?
  5. If Celsius = 100, what is Fahrenheit if Fahrenheit = ((Celcius × 9) / 5 ) + 32?
  6. If side = 14 and dimensions = 3, what is measure if measure = sidedimensions?
Get ready to code

Open up the Python programming environment, IDLE

Get Python to do all the work

Issue each command and press the  Enter  key to evaluate the expressions and compare with your answers to the questions in Step 1.

>>> widgets = 12 + 4
>>> print(widgets)

>>> fronds = 14 * (3 + 44)
>>> print(fronds)

>>> gratings = 9
>>> lines = 5
>>> zones = gratings * lines
>>> print(zones)

>>> base = 12
>>> height = 3
>>> area = 0.5 * base * height
>>> print(area)

>>> c = 100
>>> f = ((c * 9) / 5) + 32
>>> print(f)

>>> side = 14
>>> dimensions = 3
>>> measureOfSelf = side**dimensions
>>> print(measureOfSelf)

Compare the way in which you carried out the calculations in Step 1 with the way you do this in Python - can you see the similarities and the differences?

Evidence your learning

Use a combination of screenshots and written explanation to write about the connection between the mathematical notation in Step 1 and the Python implementation in Step 3.


Remember the precedence rules you use in mathematics? Python follows the same rules.
  • Brackets
  • Indices
  • Division / Multiplication
  • Addition / Subtraction

Task 2.2 Precedence
Where we learn that precedence rules apply just the same

Often, a lack of understanding of the effect of operator precedence leads to semantic / logic errors. When you look at the code, you think it's doing the correct job, whereas in fact, it's doing something different than you expect.

Look carefully at the following evaluation trees (and the difference that the brackets make) ...

Get ready to code

Open up the Python programming environment, IDLE. 

Two calculations, same numbers, different answers

Enter the following two examples, pressing the  ENTER  key after each one. Pay special attention to the results of the calculation (they will be different) - you will analyses these in the next step ...

>>> 2 + 14 * 7 - 3 ** 2
>>> (2 + 14) * (7 - 3) ** 2


In your notebooks / on paper, explain why the two calculations with the same numbers give different answers ...

OK, OK - I get it!

Think about which part of the calculations are carried out first through your knowledge of BIDMAS and what effect the brackets have on the order that the operators work ...

Impossible! - click to engage

https://docs.google.com/presentation/d/12bI670tIvI72VZxNh0rNPpv59rUWAkutA6m6jqNhA6M/preview?slide=id.g41ede566e7_0_24Activity 3
Integer and modulo arithmetic

There are three types of division in Python, float division, integer division and modulo division ...

Float or real division        : /
Integer division              : //
Modulo or remainder division  : %

Task 3.1 Different types of division
Where we investigate different types of division!

Get ready to code

Open up the Python programming environment, IDLE

At the prompt

Type each command at the prompt, pressing the
 Enter  key after each one.

 hats = 45 / 16

>>> print(hats)
>>> hats = 45 // 16
>>> print(hats)
>>> hats = 45 % 16
>>> print(hats)

Can you see what each of these operators does? Discuss your ideas with your shoulder partner.

Odd or even?

One real world use of modulo arithmetic is to determine whether a number is odd or even. Consider ...

if ... x % 2 = 0 ... x is an even number
if ... x % 2 = 1 ... x is an odd number

Choose 'File > New File' from the IDLE menu and save the blank script in a suitable place in your documents and call it 'oddOrEven.py'. Type the following script exactly as written (be careful with the indentation) ...

finished =
while not finished:
  x = int(input('Enter a whole number (0 to finish) : '))
  if x == 0:
    finished = True
    if x % 2 == 0:
      print('Number is EVEN')
    if x % 2 == 1:
      print('Number is ODD')

Now save and run the script by pressing  F5  or choosing 'Run > Run Module ...'. Try typing in a few integers and see what the script tells you.

(Tough) Programming challenges

Attempt the following challenges to demonstrate your understanding.
  • Create a Python script using IDLE (File > New File), save it as ... Task 3.1 Challenges.py
  • Use it to record your attempt at the following challenges.
  • When you have completed as many of the challenges as you can, evidence your work through a combination of screenshots and written explanations in a word processed document with a suitable header and footer. 
  • Remember to show the script and the output it generates.

Write a script which will allow you to work out the integer division (//) result and the modulus division result (%) from two values, x and y. For instance, the output of the script could look like this ...


Modulus arithmetic is sometimes known as 'clock arithmetic' because the answer to any modulus calculation 'wraps round' like the time on a clock does. Write a script which will allow you to answer questions like this ...

The calculation you must use is newTime = (startTime + interval) % 12 and we use the number 12 because there are 12 hours on a clock face. The output of the script should look like this ...


In a similar way, the days of the week wrap around as well, so newDay = (startDay + interval) % 7 and we use the number 7 because there are 7 days in a week (obviously).

Write a script which will allow you to answer questions like ...

The output of the script should look like this ...

HINTS (and you might need them!)
  • You will need to store the names of the days in a list (probably called days)
  • You will need to use the days.index(startDay) method to find the position of startDay in the list and use this in the calculation to find newDay.
  • You will need to use days[newDay] to find the name of the new day so you can print it out.

Assessment Task (Homework)

Do some research to find out about two other real world examples of the modulus / remainder / % function.

Grading rubric

MASTER :  You have found out at least three interesting uses of modulo arithmetic and explained them beautifully.
APPRENTICE You have found out two interesting uses of modulo arithmetic and explained them simply.
NOVICE : You have found out one interesting use of modulo arithmetic but not really explained it.

# Flash cards.png
Click to load key word list to help you make your own flash cards 

Hungry for more?

Modulo arithmetic is extensively used during the calculation of check digits for barcodes. Do some research into the algorithms involved in check digital calculations.

You could play a couple of games of the Countdown numbers game - can you get Python to do the maths for you or, even better, could you write a Python program to solve the puzzle?