I made a simple print
function that just check if 4 * 5 = 20 is True
.
Why am I getting False
?
result = 20
Num1 = 4
Num2 = 5
print(Num1*Num2 == result is True)
I made a simple print
function that just check if 4 * 5 = 20 is True
.
Why am I getting False
?
result = 20
Num1 = 4
Num2 = 5
print(Num1*Num2 == result is True)
Check:
result = 20
Num1 = 4
Num2 = 5
print(Num1*Num2 == result is True)
#False
print((Num1*Num2 == result) is True)
#True
print((Num1*Num2) == (result is True))
#False
You could solve it by putting brackets in the right places.
Num1*Num2 == result is True
is an example of operator chaining, and is equivalent to:
(Num1*Num2 == result) and (result is True)
This is similar to how 1 < x < 10
is equivalent to 1 < x and x < 10
. It happens because both ==
and is
are considered comparison operators, and two comparison operators in sequence are chained in this manner.
Since result is True
returns False, the overall result is false.
In general, you shouldn't write x is True
- just write x
. Here: print(Num1*Num2 == result)
.