+ 5

Difference of operator precedence between C and Python

Hi, everyone who loves both C and Python, did you ever notice the differences of operator precedence between C and Python? If you know one that matters much when coding Python for a C programmer, please post below this thread. I notice some intricate differences on the following. In Python, the so-called relationship operators such as “>,<,==,!= ” have the same precedence, while in C the precedence of “<,>” have higher precedence than that of “==,!=”. Additionally, in Python, the so-called comparison operator such as the relationship operator can be chained. For example, the following expression 1 >2 == 0 evaluates to true In C, while False In Python. In Python: https://code.sololearn.com/c7GBcv4F8DIi/?ref=app In C: https://code.sololearn.com/c53o7u0EodU4/?ref=app

31st May 2019, 6:03 AM
slowweb
13 Réponses
+ 5
Ipang, both are wrong. Like slowweb and HonFu explained, we call it chaining of comparison operators. In mathematics, when we say 2>1>0, we mean "2>1 and 1>0". Same happens here in Python. So 4>2==0 (4>2) and (2==0) True and False False https://docs.python.org/3/reference/expressions.html#comparisons
31st May 2019, 8:27 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 6
Thank you Kishalaya Saha 👍 I'm much accustomed to C/C++ explicit way for expression evaluation, and forgotten that chained operator from Python. It is one that confuses many Python learners when they try C/C++.
31st May 2019, 8:43 AM
Ipang
+ 6
Without this thread, I may never thought about 3>2==1 😂
31st May 2019, 11:30 AM
Flandre Scarlet
Flandre Scarlet - avatar
+ 5
1 < 2 == 0 evaluates to False in both C and Python. However, 4 > 2 == 2 evaluates to True in Python, but False in C.
31st May 2019, 6:36 AM
Diego
Diego - avatar
+ 5
In Python 4 > 2 == 0 translates to 4 > 2 and 2 == 0.
31st May 2019, 7:42 AM
HonFu
HonFu - avatar
+ 5
Ipang you're so welcome! It is indeed confusing, especially when used with the == operator (to me at least).
31st May 2019, 9:18 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 4
Diego How 4 > 2 == 0 is evaluated in C and Python?
31st May 2019, 7:02 AM
Ipang
+ 4
4>2 == 0 is evaluated as “ (4>2) and (2==0) ”in Python, while as “(4>2) == 0”in C. The Python case involves chained operator.
31st May 2019, 7:40 AM
slowweb
+ 3
(4 > 2) == 0 (True) == 0 True == 0 ==> False ??? 4 > (2 == 0) 4 > (False) 4 > False (0) ==> True ??? Which one is right? or both is wrong?
31st May 2019, 7:50 AM
Ipang
+ 3
slowweb Please reconsider one of the other responses for best answer mark, I came here only to seek for clarity, and found one, so my question of doubt shouldn't count for answer ✌
31st May 2019, 3:08 PM
Ipang
+ 2
sorry, I have make a typos, the example should involve “1>2==0”
31st May 2019, 6:49 AM
slowweb
+ 1
It seems in Python the quality operator “==” could be used for any two objects, regardless of object type. For example, the following codes are valid in Python: print('aaaa' == 3)
1st Jun 2019, 7:20 AM
slowweb
+ 1
你是中国人吗?
14th Jun 2019, 9:16 PM
洗衣液
洗衣液 - avatar