1

Teaching myself coding, what is the order of operations for this line of code?

print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

I attempted to do remainder and division first so I got 3 + 2 + 1 - 5 + 0 - 0.25 / 4 + 6. Then I completed AS from left to right and got 0.075. Totally wrong because LPTHW puts it at 7. Please offer detailed operation order.

I Googled Python order of operation, but results are not too instructively detailed.

print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

Expected result is 7, but obtained 0.075

RoiMinuit
  • 404
  • 4
  • 17
  • 2
    Possible duplicate of [How do order of operations go on Python?](https://stackoverflow.com/questions/48937457/how-do-order-of-operations-go-on-python) – President James K. Polk Aug 04 '19 at 01:15
  • [Operator precedence documentation](https://docs.python.org/3/reference/expressions.html#operator-precedence) – D Malan Aug 04 '19 at 01:15
  • I got `6.75` in Python 3.6 and `7` in Python 2.7. Not sure how you got `0.075`. Is the `<<` supposed to be part of your expression? – D Malan Aug 04 '19 at 01:20
  • @Carcigenicate to the division sign, yes I forgot to remove it. From the book, the result is 7 and when I run the code I also get 7. I am attempting to do it mentally to see how the operations work. – RoiMinuit Aug 04 '19 at 01:21
  • @JohnColeman Copish!! Will remeber that. However, the author of LPTHW seems to be a pretty detailed and thorough instructor. I think he makes us do it that way to teach us the importance of parentheses in the future? I don't know... – RoiMinuit Aug 04 '19 at 01:22
  • @DelenaMalan in Python, I got 7 indeed. But mentally, or rather down on paper, I got 0.075... – RoiMinuit Aug 04 '19 at 01:23

3 Answers3

5

It depends on which Python version you are using.

In Python 2, the / operator defaults to integer division, so 1 / 4 == 0.

On the other hand, in Python 3, the / operator defaults to true division, so 1 / 4 == 0.25. You must use // to achieve integer division in Python 3.

Regardless, Python still follows the classical PEMDAS order of operations, so the modulus and division still happen first, followed by addition and subtraction from left to right.

Here's how the problem reduces after you do the modulus and division in both versions:

Python 2

(3 + 2 + 1 - 5 + 0 - 0 + 6) == 7

Python 3

(3 + 2 + 1 - 5 + 0 - 0.25 + 6) == 6.75

Jacob Marshall
  • 182
  • 2
  • 11
  • Thank you so very much! Very well detailed answer indeed! Now I can proceed with my lesson! – RoiMinuit Aug 04 '19 at 01:45
  • @WitcherJean if this answers the question for you, you can mark it as the answer by ticking the check mark left of the post. (Or do that with whichever answer you feel best answered the question) – BruceWayne Aug 04 '19 at 02:09
  • 1
    @BruceWayne Haa! Thanks, didn't know how to do that actually! – RoiMinuit Aug 07 '19 at 21:26
1

In Python 2, / uses integer division if both its arguments are integers. That means that 1/4 == 0, since integer division will round down. Then:

= 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 
= 3 + 2 + 1 - 5 + 0 - 0 + 6
= 7

To get 6.75 in Python 2 (the expected answer when done on paper), make one of the operands a float:

>> 3 + 2 + 1 - 5 + 4 % 2 - 1.0 / 4 + 6
#                          ^
>> 6.75

This isnt necessary in Python 3 because / defaults to returning a float.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
1

*, /, //, % have higher precedence than + and -. So you should first calculate 4 % 2 = 0 and 1 / 4 = 0 (in Python 2.7), and then do the rest of the calculation from left to right.

D Malan
  • 10,272
  • 3
  • 25
  • 50