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 ...
So that we can ...
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
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 symbols are used for multiplication, division and exponentiation in programming ...
Write 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 calculator Where we learn how to use Python as a simple calculator
Answer the following questions, jotting down your ideas in your folders.
Open up the Python programming environment, IDLE
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 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?
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.
BIDMAS
Remember the precedence rules you use in mathematics? Python follows the same rules.
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) ... Open up the Python programming environment, IDLE.
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 ...
Task 3.1 Different types of division Where we investigate different types of division!
Open up the Python programming environment, IDLE
>>> 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. 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) ...
while not finished: x = int(input('Enter a whole number (0 to finish) : ')) if x == 0: finished = True else: 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.
Attempt the following challenges to demonstrate your understanding.
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?
|