+ 1
Why this code prints output yes ?
x=True y=False z=False if x or y and z: print("yes") else: print("no!") this is how i thought it is evaluated x or y and z True and z that is. True and False results to False where am i wrong?
4 ответов
+ 10
with "or" if one operand is true then all the statment is true.
you can see the above expression as below:
x or ( y and z )
=> true or ( false and false)
=> true or false
=> true
we get that the statement is true, then it will print "yes"
+ 4
According to Python documentation, the operators precedence as follow:
First is 'not x', then 'and', then 'or'.
Reference (see towards the page bottom):
https://docs.python.org/3/reference/expressions.html
+ 2
Operator precedence, the AND is executed first then the OR.
First AND then OR then NOT.
+ 1
cx=True
y=False
z=False
if x and y or z:
print("yes")
else:
print("no!")
>>> now it will print no.