PYTHON BODMAS
Week A16: 17th-23rd Feb2025
2 min readFeb 24, 2025
- B — Bracket (parenthesis)
- O — Order ( exponent)
- D — Division
- M — Multiplication
- A — Addition
- S — Subtraction
BODMAS is just a trick or concept to remember the precedence order in mathematics.
Get write_avis’s stories in your inbox
Join Medium for free to get updates from this writer.
This is precedence order and the operations associvity
- parenthesis `()` has highest precedence (level 1) and works from inwards to outwards parenthesis in nested parenthesis, and has left to write association
- exponent `**` has second highest precedence (level 2) and right to left association
- unary operations like `+x, -x, ~x` has next level precedence (level 3) and right to left association
- division `\`, multiplication `*`, floor division `\\`, modulus `%` has same precedence (4) and left to right association.
- addition `+` and subtraction `-` has level 5 precedence and left to right association.
Some notes
- Python follows left to right order in most cases.
- unary operations means it only affects one operand unlike other operations which operates between two operands
- unary operations `x = 5 then -x ==> -5 `
- subtraction operation `x = 5 , y = 3 then x-y ==>2`
# SIMPLE ADDITION
1+2+3+4+5
output
>>> 15
* It has left to right association so,
* It goes 1+2 →3 +3 →6 +4 →10 +5 →15
# SIMPLE SUBRACTION
10-2-3-4-5
output>>> -4
* It also has left to right association
* It goes 10-2-->8 -3 -->5 -4 -->-1 -5-->-4
# SIMPLE MULTIPLICATION
1*2*3*4*5
output>>> 120
* It goes 1*2-->2 *3-->6 *4 -->24 *5 -->120
* It also has left to right association
# SIMPLE DIVISION
24/4/3/2
output>>> 120
* It goes 24/4 -->6 /3 -->2 /2-->1
* It also has left to right association
# SIMPLE EXPONENTIAL
2**3**2
output>>> 512
* It goes 2**(3**2)--> 2**9 -->512
* It has right to left association
# SOLVING COMPLEX EXPRESSIONS utilising above simple calculation
2 + 3**2**2 /3 -(5 *3) - 6 / 10 * 4/30 *25 + 5 - 10
output>>> 7
series of conversions
step1: evaluates paranthesis `5*3 = 15` then the above expression becomes
2 + 3**2**2/3 -15 -6 / 10 * 4 / 30 * 25 + 5 - 10
step2: evaluates exponential from right to left `==> 3**2**2 ==>3**4==>81`
2 + 81/3 -15 -6 /10 * 4 / 30 * 25 + 5 - 10
step3: evaluates multiplication and division from left to right as
they have same precedence and
`81/3 ==>27`, `6/10 = 0.6`, `0.6*4 = 2.4`, `2.4/30 = 0.08`,`0.08*25 = 2`
the eqn becomes
2 + 27 -15 - 2 + 5 -10
step4: evaluates addition and subraction from left to right
`2 + 27 = 29`, `29-15 = 14`,`14-2 = 12`, ` 12+5 = 17` , `17-10 = 7`
therefore the final result is 7





