5. Arithmetic

5.1. Big question

  • How do I got the computer to do the arithmetic for me?

5.2. Key words

  • Add
  • Subtract
  • Multiply
  • Divide
  • Division Remainder (also called modulo).

5.3. Introduction (Task 1)

In arithmetic class, you are asked to do calculations such as “What is 6×7?”. In computing class, you will never be asked to do this.

_images/rectangle.png

If I ask “What is the area of the rectangle?”, then don’t tell me a number. Use the computer to do the multiplication.

    Arithmatic-1.1: So what is the area of the rectangle?
  • 42cm²
  • I told you not to do the multiplication, that is the computers job.
  • 36cm²
  • No, that is not correct, the computer is much better than us at multiplying and does not make mistakes.
  • 6×7cm²
  • Yes, we can then get the computer to do the rest.
  • 6×7cm
  • No, the units are wrong. What is cm×cm?
  • 6×8cm²
  • No, look at the image again, what are the numbers?

5.4. Arithmetic (Task 2)

Here are some other arithmetic operators.

Operator Meaning Example Result
    Math Python  
+ add 4 + 6 4 + 6 12
- subtract 9 - 3 9 - 3 6
* multiply 4 × 9 4 * 9 36
/ divide 14 ÷ 5 14 / 5 2.8
// integer divide 14 ÷ 5 14 // 5 2
% division remainder   14 % 5 4
** exponentiation 4**2 16

As you can see, it is mostly the same as maths, except that we use * for multiply, / for division (sometimes this is used in maths), ** for exponentiation, and % for remainder. We also have two types of division.

    maths-2.2 Drag and match. Have you done the ones that you know? Have you discussed with another pair?
  • 3+4
  • Three plus four is
  • 7-2
  • Seven minus two is
  • 4*5
  • Four multiplied by five is
  • 360/3
  • Three hundred and sixty divided by 3 is
  • 5**2
  • five squared is

5.5. Now to try it out (Task 3)

To see the answers in python, you will need to print them. In the old days this would send them to the printer, but now we have video display screens instead of printers.

  1. Press Run, to see what happens.
  2. Add your own, and run again.
  3. Add more, play, have fun.
  4. Can you use more than one symbol?

5.6. Bidmas (Task 4)

    Arithmatic-1.4: What is 2+2*2?
  • 5
  • No, remember bidmas
  • 6
  • Yes, python uses bidmas. Now try it.
  • 7
  • No, remember bidmas
  • 8
  • No, remember bidmas, multiplication is done before addition.

5.7. Bidmas (Task 5)

    Arithmatic-1.5: What do we do if we want the addition done first?
  • 2+(2*2)
  • No, remember bidmas: brackets, indices, multiply or divide, add or subtract
  • 2*2+2
  • No, remember bidmas: brackets, indices, multiply or divide, add or subtract
  • (2+2)*2
  • Yes, “we will always have bidmas”. Now try it.
  • (2+2*2)
  • No, remember bidmas: brackets, indices, multiply or divide, add or subtract

As well as printing the result, you can use it. See these examples.

5.8. Use with turtle (Task 6)

5.9. Use with turtle (Task 7)

Next Section - 6. Procedures again