Open hillairet opened 9 months ago
It's all explained by the operation precedence
Hmmmm
>>> 1 + (-(+(-(+1)))) # 1 + 1
2
>>> 1 + (+(+(+(+(+1))))) # 1 + 1
2
>>> 1 - (-(-1)) # 1 - 1
0
>>> 1 - (-1) # 1 - (-1) == 1 + 1
2
>>> +5 // 2
2
>>> -5 // 2
-3
>>> 2 + (5 // 2) # 2 + (+5 // 2)
4
>>> 2 - ((-5) // 2) # 2 - (-5 // 2)
5
>>> 2 + ((-(-5)) // 2) # 2 + (+5 // 2)
4
>>> 2 - ((+(-5)) // 2) # 2 - (-5 // 2)
5
>>> 2 - ((-(+5)) // 2) # 2 - (-5 // 2)
5
Thank you for the breakdown with parenthesis @shiracamus. I'm not too sure what to make of the comment "Hmmmmm" though.
There is clear operator precedence. Taking just this example:
>>> 2 - ((-(+5)) // 2) # 2 - (-5 // 2)
5
The 2 signs -
and +
are applied to the 5 first, then //
is performed, then the subtraction with 2 -
. That matches the operator precedence table.
I'm not too sure what to make of the comment "Hmmmmm" though.
Its meaning is ``I try using parentheses to check the priority.''
Hello. Cool collection of WTFs I made a blog post about something that belongs in this collection.
I'd be happy to write up a section for it. Where should I put it? Any suggestion? Maybe right before or after the "Not knot" section that also deals with order precedence?
TL;DR This is all valid python:
It's all good an dandy until you use it with
//
and then things get weird:It's all explained by the operation precedence but it's very counter intuitive!