Time to learn about the different mathematical methods that we can use during our programming. Some you will recognise, some you wont.
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)
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 operatorWhere we learn that mathematical operators in programming look different
Do you recognise these?Look at the following list of mathematical operators.
Look for the
multiplication symbol, the division symbol and some way of expressing exponentiation on your computer keyboard. Can you find them?
Different symbolsDifferent symbols are used for multiplication, division and exponentiation in programming ...
In your notebook / on paperWrite down the
complete list of programming mathematical operators (in blue) and explain what function each of them performs.Some things are!
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 calculatorWhere 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.
- If
*widgets*= 12 + 4, what is*widgets*? - If
*fronds*= 14 × (3 + 44), what is*fronds*? - If
*gratings*= 9 and*lines*= 5, what is*zones*if*zones*=*gratings*×*lines*? - If
*base*= 12 and*height*= 3, what is*area*if*area*= 0.5 ×*base*×*height*? - If
*Celsius*= 100, what is*Fahrenheit*if*Fahrenheit*= ((*Celcius*× 9) / 5 ) + 32? - If
*side*= 14 and*dimensions*= 3, what is*measure*if*measure*=*side*?^{dimensions}
Get ready to codeOpen up the Python programming environment, IDLE
Get Python to do all the workIssue 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` 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 learningUse 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.BIDMASRemember the precedence rules you use in mathematics? Python follows the same rules.
- Brackets
- Indices
- Division / Multiplication
- Addition / Subtraction
Task 2.2 PrecedenceWhere 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 codeOpen up the Python programming environment, IDLE.
Two calculations, same numbers, different answersEnter 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`
Why?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 ...
Task 3.1 Different types of divisionWhere we investigate different types of division!
Get ready to codeOpen up the Python programming environment, IDLE
At the promptType 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 numberif ...
`x % 2 = 1` ... x is an odd numberChoose '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 = False` `while not finished:` ` x = int(input('Enter a whole number (0 to finish) : '))` ` if x == 0:` ` finished = True` ` else:` ` if ` ` print('Number is EVEN')` ` if ` ` 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 challengesAttempt 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.
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? |