# BODMAS

BODMAS, BIDMAS, BEDMAS, PEMDAS
© Coventry University. CC BY-NC 4.0

In the previous step, we used mathematical expressions to transform raw data values. When looking at more complex mathematical expressions, we need to be crystal clear about exactly how the computer will interpret them.

In a mathematical expression that involves a mixture of operations (such as addition, subtraction, multiplication, division and powers), it’s important to know how humans and computers interpret and apply the operations to evaluate the expression. If a mathematical expression is the input, there is one and only one possible result.

## What’s the problem with mathematical expressions?

For motivation, compare (3+5times7). If we apply the operations strictly from left-to-right, we would get (3+5=8) and (8times7), which gives us 56 as the final answer. However, you might remember from school that multiplication has a higher precedence than addition, so instead we evaluate (5times7=35) first and then (3+35), which gives the final answer of 38.

## What is BODMAS?

From your school days, you may remember BODMAS (sometimes called BIDMAS, BEDMAS or even PEMDAS). This is an acronym to help you remember the precedence of basic mathematical operations:

• B – brackets (sometimes called parentheses)
• O – order (sometimes called indices, exponents or powers)
• DM – division and multiplication
• AS – addition and subtraction

Brackets (B) have the highest precedence as they override all other operations. The second highest precedence is order (O) which is the ‘to the power of’ operation. Division and multiplication (DM) have equal precedence to each other, hence being grouped together in the acronym. Finally, we have addition and subtraction (AS) which have equal precedence to each other. Operations with the same precedence are evaluated left-to-right.

Let’s look at some examples (as a human) and then we can check we have understood correctly by evaluating the expression in Python. We’ll start from the bottom of BODMAS, at addition and subtraction, and work our way up to the top, brackets.

The expression (7+5+3+2) is evaluated left-to-right since there is only one operator, addition. The result is 17. In Python, this would look as follows.

7+5+3+2

The expression (7-5+3-2) is also evaluated left-to-right, but this time has two operators, addition and subtraction, both of which have the same precedence. The result is 3. In Python, this would look as follows.

7-5+3-2

The expression (7+5times3+2) includes multiplication, which has a higher precedence than addition. This means we first carry out (5times3) giving 15 and then (7+15+2), which gives the final answer of 24. In the Python example below, notice * is used as the multiplication symbol.

7+5*3+2

The expression (7+5times3^2) includes order, which has a higher precedence than addition and multiplication. This means we first carry out (3^2) which gives 9. The remaining expression (7+5times9) has multiplication, which has a higher precedence than addition, so we carry out (5times9), which gives 45. We then do the addition element of the expression (7+45), which gives 52 as the final answer. In the Python example below, notice ** is used as the power symbol.

7+5*3**2

The expression (7+(5times3)^2) has brackets, which has the highest precedence of all operations. So, we first carry out (5times 3), which gives 15. Then (7+15^2) has order as the highest precedence, so we carry out (15^2), which gives 225. Finally, (7+225) gives 232 as the final answer. In Python, this would look as follows:

7+(5*3)**2

A supposedly more simple calculation is 1 divided by 2/3. It’s all division so it’s simple, right? Not quite, this would actually be 1/(2/3), not 1/2/3, which you might assume. This gives (0.dot{6}7) as the final answer, rather than (0.1dot{6}7).

Data science constantly involves logical thinking and attention to detail, as you’ve seen above. The Python interpreter applies the rules of precedence when evaluating conditions and expressions. Whenever you might be unsure about how an expression will be evaluated, you can check using some examples in Python and, subsequently, use more brackets to make it clear to a human reader.

Advanced ideas. The concept of precedence of operations can be extended to other types of operations such as integer division (quotient and remainder), comparison operations (less than, greater than, equal to) and logical operations (and, or, not). We will not be looking at these types of operations in this course, but you will likely come across them in the future. If you’d like to find out more, have a look at the Python operator precedence table.

Every Python expression has exactly one possible interpretation. When evaluating an expression, the computer follows the precedence of operations, which we just refer back to the acronym BODMAS.
Consider the expression (3315times2div3div5times7times11div13). Using the above as a guide, calculate the answer as a human and then check it using Python (using / as the division symbol).
If you need help to understand how to calculate it or why the answer is what it is, ask your fellow learners in the comments area below.

BBC Bitesize. (n.d.). KS3: Order of operation. https://www.bbc.co.uk/bitesize/guides/zsnycdm/revision/1

Tutorials Point. (n.d.). Python operators precedence example. https://www.tutorialspoint.com/python/operators_precedence_example.htm

© Coventry University. CC BY-NC 4.0