0% found this document useful (0 votes)
1 views24 pages

Arithmetic

This lesson covers evaluating arithmetic expressions using operator precedence rules, specifically through the BIDMAS acronym. It explains how to use arithmetic operators in Python, including addition, subtraction, multiplication, division, integer division, and modulo. The lesson also includes examples and questions to reinforce understanding of these concepts.

Uploaded by

19koudasm.ps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views24 pages

Arithmetic

This lesson covers evaluating arithmetic expressions using operator precedence rules, specifically through the BIDMAS acronym. It explains how to use arithmetic operators in Python, including addition, subtraction, multiplication, division, integer division, and modulo. The lesson also includes examples and questions to reinforce understanding of these concepts.

Uploaded by

19koudasm.ps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Computing

Lesson 2: Arithmetic Expression

Programming Part 2: Selection

Rebecca Franks

Materials from the Teach Computing Curriculum created by the National Centre for Computing Education
In this lesson you will:

Evaluate arithmetic expressions


using rules of operator precedence

Write and use expressions that use


arithmetic operators

Assign expressions to variables

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

10-2+2*5 There are no brackets, indices, or division in


() this expression, so start with multiplication.
42

/ *

+ -

6
BIDMAS
BIDMAS

10-2+2*5 There are no brackets, indices, or division in


() this expression, so start with multiplication.
42
2*5 is 10
/ *

+ -

7
BIDMAS
BIDMAS

10-2+2*5 There are no brackets, indices, or division in


() this expression, so start with multiplication.
10-2+10 42
2*5 is 10
/ *

+ -

8
BIDMAS
BIDMAS

10-2+2*5 There are no brackets, indices, or division in


() this expression, so start with multiplication.
10-2+10 42
2*5 is 10
/ *

+ -

9
BIDMAS
BIDMAS

10-2+2*5 Add and subtract should be read from left to


() right. If the subtract appears first, then this
10-2+10 42 should be carried out first.
/ * 10-2 is 8
+ -

10
BIDMAS
BIDMAS

10-2+2*5 Add and subtract should be read from left to


() right. If the subtract appears first, then this
10-2+10 42 should be carried out first.
8+10 / * 10-2 is 8
+ -

11
BIDMAS
BIDMAS

10-2+2*5 Finally, you are left with one operator and


() perform this final calculation.
10-2+10 42
8 + 10 is 18
8+10 / *

+ -

12
BIDMAS
BIDMAS

10-2+2*5 Finally, you are left with one operator and


() perform this final calculation.
10-2+10 42
8 + 10 is 18
8+10 / *

+ -
18

13
Using operators in Python
Here is a list of arithmetic operators that + Addition
can be used in Python.
- Subtraction

* Multiplication

/ Real division

// Integer division (quotient)

** Powers

% Modulo (MOD)

14
Using operators in Python
Integer division and modulo are possibly + Addition
unfamiliar to you.
- Subtract

* Multiplication

/ Real division

// Integer division (quotient)

** 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
>>>

Note: The MOD doesn’t simply store the value that


was discarded from integer division. It stores the
whole number that was remaining.

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

You might also like