Arithmetic
Arithmetic
Rebecca Franks
Materials from the Teach Computing Curriculum created by the National Centre for Computing Education
In this lesson you will:
2
Arithmetic expressions
Question .
What value will be held by the variable
number = 10-2+2*5
number?
A. -2
B. 18
C. 50
D. 30
3
BIDMAS
Arithmetic expressions are evaluated in order B - brackets
of operator precedence.
I - indices
You can use BIDMAS to help remind you of
D - division
the correct order.
M - multiplication
It is important to note that add and subtract
are interchangeable and should be evaluated A - addition
from left to right.
S - subtract
4
BIDMAS
In your maths lessons you might have seen
BIDMAS represented in a triangle like this ()
diagram.
42
/ *
+ -
5
BIDMAS
BIDMAS
/ *
+ -
6
BIDMAS
BIDMAS
+ -
7
BIDMAS
BIDMAS
+ -
8
BIDMAS
BIDMAS
+ -
9
BIDMAS
BIDMAS
10
BIDMAS
BIDMAS
11
BIDMAS
BIDMAS
+ -
12
BIDMAS
BIDMAS
+ -
18
13
Using operators in Python
Here is a list of arithmetic operators that + Addition
can be used in Python.
- Subtraction
* Multiplication
/ Real division
** Powers
% Modulo (MOD)
14
Using operators in Python
Integer division and modulo are possibly + Addition
unfamiliar to you.
- Subtract
* Multiplication
/ Real division
** Powers
% Modulo (MOD)
15
Using operators in Python
Here is an example of how real division will 1 number = 14/3
output. 2 print(number)
3
In real division there is no remainder
because the entire value is divided.
4.666666666666667
>>>
16
Using operators in Python
When you use integer division, it will 1 number = 14//3
discard the decimal part. 2 print(number)
3
Integer division is the operation that
calculates how many whole times the divisor
(3) will fit in the dividend (14).
4
>>>
17
Using operators in Python
Modulo (MOD) is used to work out the 1 number = 14%3
remainder of the division. 2 print(number)
3
2
>>>
18
Using operators in Python
If you divide 14 counters between 3 people, 1 counters_each = 14//3
then there will be 4 counters each with 2 2 counters_remaining = 14%3
remaining. 3 print(counters_each)
4 print(counters_remaining)
4
2
>>>
19
Using operators in Python
You would use modulo when you need to 1 number = 27
find out the remaining whole value. 2 odd_even = number%2
3 print(odd_even)
This will be helpful in many cases. An
example might be when you wish to find out
if a value is odd or even.
1
If the value evaluates as a 1, then it is odd. >>>
20
Using operators in Python
If the value evaluates as a 0, then it is even. 1 number = 26
2 odd_even = number%2
3 print(odd_even)
0
>>>
21
Integer division and modulo
Question .
What value will be held by the variable
number = 25%4
number?
A. 6.25
B. 1
C. 6
D. % is not a valid operator so this will
cause an error message.
22
Integer division and modulo
Question .
What value will be held by the variable
number = 10/3
number?
A. 3.3333333333333335
B. 3
C. 1
D. / is not a valid operator so this will
cause an error message.
23
Integer division and modulo
Question .
What value will be held by the variable
number = 13//3
number?
A. 1
B. 4.333333333333333
C. 4
D. // is not a valid operator so this will
cause an error message.
24