+ 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?

3rd May 2017, 5:50 AM
Jilson Joseph
4 odpowiedzi
+ 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"
3rd May 2017, 6:07 AM
Mohammad Dakdouk
Mohammad Dakdouk - avatar
+ 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
10th May 2017, 3:56 PM
Kosai Al Khateb
Kosai Al Khateb - avatar
+ 2
Operator precedence, the AND is executed first then the OR. First AND then OR then NOT.
3rd May 2017, 6:02 AM
Ghauth Christians
Ghauth Christians - avatar
+ 1
cx=True y=False z=False if x and y or z: print("yes") else: print("no!") >>> now it will print no.
6th May 2017, 2:50 PM
রয়েল
রয়েল - avatar