+ 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
7 Answers
+ 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).
+ 6
https://www.sololearn.com/discuss/2253416/?ref=app
Thanks to HonFu ☺️
+ 2
Zuke
RAJESH SAHU
Thanks for the answer. 😉
+ 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.
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.
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.. 💛