2

I thought I understood BODMAS (BIDMAS in the UK). I'm unsure why the following expression evaluates in a different order:

a = 8*4//3

I expected 'division' to take place first, giving a = 8*1 - instead 'multiplication' occurs first, giving a = 32//3 = 10

b = 9//3*7

Example b evaluates to 21, as per BIDMAS rule.

It seems that python executes an expression from left to right and treats 'division' and 'multiplication' as equivalent. What's happening? Thanks in advance, Faz.

Darek Kay
  • 15,827
  • 7
  • 64
  • 61
Mr malik
  • 21
  • 1
  • 1
  • 3
  • 1
    no, `*` and `/` have the same priority. So, the symbol appears first would be evaluated first. – Avinash Raj May 17 '15 at 11:01
  • Operations of equal rank *are* evaluated from left to right by [the "BODMAS" rule](https://www.mathsisfun.com/operation-order-bodmas.html)... – Tim Pietzcker May 17 '15 at 11:01
  • 1
    Division and multiplication have the same precedence, with the leftmost operator winning the tie. It is the same for addition and subtraction. Even in BODMAS, multiplication/division are associative, it's just that when you write the acronym, one has to come before the other. The choice is arbitrary. – Asad Saeeduddin May 17 '15 at 11:02
  • 1
    [Python Docs | Operator precedence](https://docs.python.org/2/reference/expressions.html#operator-precedence) – Lukas Graf May 17 '15 at 11:03
  • It's BIMDAS in the south east of UK by the way, so that does make sense to me – Clive May 17 '15 at 11:04

4 Answers4

3

In BEDMAS, BIDMAS, BOMDAS, BODMAS, PEDMAS, and other order of operations acronyms, division and multiplication carry the same weight and are calculated from left to right within their current block.

Read this section for clarification.

Oka
  • 23,367
  • 6
  • 42
  • 53
1

If you do a multiplication and a division it doesn't matter which order you do it in for example 6*5/3 = 10 the same as 6/3*5 = 10. The difference comes with addition where 6+3*5 = 21 but (6+3)*5 = 45.

Drew
  • 11
  • 1
  • Not entirely; try changing the 3 to 4, in Python2 you get 6 * 5 / 4 = 7, 6 / 4 * 5 = 5; Python3 6 * 5 // 4 = 7, 6 // 4 * 5 = 5. – Mark Nov 11 '18 at 10:28
1

Turning the comments on the OP into an independent answer, as this post shows up at the top of search results:

Multiplication & Division have equal precedence in Python, as well as according to the BODMAS/BEDMAS/BIDMAS/BIMDAS/BOMDAS/PEDMAS rule, so the evaluation then proceeds from Left to Right Python2 docs

Mark
  • 2,196
  • 1
  • 14
  • 8
0

My 1C here:

This is the formula for order of precedence:

  1. () : Parenthesis First!
  2. ** : Exponentiation
  3. *, @, /, //, % : all same precedence L to R
  4. +, - : all same precedence L to R

Example: problem under question: 8//3*3/2+10%2**2 ; This is solved in steps below using above formula.

  1. print(8//3*3/2+10%2**2)
  2. print(8//3*3/2+10%4) # Exponentiation applied (2 above)
  3. print(2*3/2+10%4) # (3 above)
  4. print(2*3/2+10%4) # (3 above)
  5. print(6/2+10%4) # (3 above)
  6. print(3+10%4) # (3 above)
  7. print(3+2) # (4 above)

#5.0 Final answer

List

Srini_N
  • 1
  • 1