Today, we are going to talk about basic arithmetic operations in Python . BODMAS rule stands for Bracket Of Division, Multiplication, Addition and Subtraction . It explains the order of operations to solve an expression.
Similar to BODMAS rule , Python follows PEDMAS rule . It stands for Parentheses, Exponents, Division, Multiplication, Addition and Subtraction . PEDMAS is a synonym of BODMAS .
If an expression contains brackets like (), {}, [] , first we have to solve the bracket . Second division, multiplication, addition and subtraction from left to right. If solved in a wrong order will result in wrong result .
Simple Demonstration :
1 2 3 4 5 |
// bodmas.py result = 8 + 5 * ( 2 * 9 / 3 * (2 * 1) ) + 2 - 5 print(result) // 65.0 |
Above example will result 65.0 . Now, Let’s look at the math :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
8 + 5 * ( 2 * 9 / 3 (2 * 1) ) + 2 - 5 8 + 5 * ( 2 * 9 / 3 * 2 ) + 2 - 5 8 + 5 * ( 2 * 3 * 2 ) + 2 - 5 8 + 5 * 12 + 2 - 5 8 + 60 + 2 - 5 70 - 5 65 |
This is just a simple demonstration of PEDMAS/BODMAS in Python . Hope this was helpful to you 🙂 Happy Coding 🙂