+ 10

Interesting python ( Any PY Geeks out there)

I recently started learning my sixth programming language and it's PYTHON 🐍. I got something interesting here. #Code print(True!=0==True) #Output : False Discussion : If I work without checking the precedence of the operators ( != and == ), then there are two cases possible as -- 1. If != executes first (True!=0)==True (True) == True True 2. If == exexutes first True!=(0==True) True! =(False) True In both the cases, the output should be True. Even then the output is False. I will add some of the output which may be helpful to understand the problem -- print(True!=0) #True print(True!=False) #True print(0==True) #False print(True==True) #True I find it interesting and would love to know why is this False in python

21st Apr 2020, 6:01 PM
Akshay Jain
Akshay Jain - avatar
7 Réponses
+ 7
Interesting riddle. Neat way to learn python 👍 This is what happens: True != 0 == True --> True != 0 and 0 == True --> False From the docs: Formally, if a, b, c, …, y, z are expressions and op1, op2, …, opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once. Note that a op1 b op2 c doesn’t imply any kind of comparison between a and c, so that, e.g., x < y > z is perfectly legal (though perhaps not pretty).
21st Apr 2020, 6:33 PM
Zuke
Zuke - avatar
21st Apr 2020, 6:55 PM
ANJALI SAHU
+ 2
Zuke RAJESH SAHU Thanks for the answer. 😉
22nd Apr 2020, 8:29 AM
Akshay Jain
Akshay Jain - avatar
+ 1
ugochukwu Joseph Bro your statement is totally wrong that digit below 1 is false.. Actually only 0 is the digit which is false else all non-zero digits are true. As this code runs perfectly even with negative value in if block -- if -1 : print('This code is running perfectly') But according to you(negative values are false), it shouldn't have run. If you compare with boolean values, then True is equal to 1 only and False is equal to 0 only. And I already noted that 0==True will return False. Here, the question is different.. so I would suggest that you should read the question carefully.
22nd Apr 2020, 5:05 PM
Akshay Jain
Akshay Jain - avatar
0
there are two expressions(true != 0, 0 == true) in this statement that both are checked once and the output given is from the last expression(0 == true) #false.
22nd Apr 2020, 9:10 AM
Ajith kumar Chermaraja
Ajith kumar Chermaraja - avatar
0
The output will be false no matter what is in the middle of the equation. eg (True=!foobar==True) because logically a thing cannot be both true and not true.. 💛
24th Apr 2020, 3:13 PM
madeline
madeline - avatar