+ 4
Explain answer to question in challenge
Please could you tell me why this question has this answer? What's the output? a = True b = False c = False if a or b and c: print('yes') else: print('no') My and opponent's answer: 'no' Correct answer: 'yes' I'm obviously missing something, don't you know what? Thanks before
6 Respostas
+ 10
operator priority. first: b and c(=false), then a or false = true
+ 3
Julia Shabanova Steven M Thank you both. Can you tell me why the python interpreter reads the AND operator first? Is it just decision of the makers of Python or is there any reason behind it?
+ 2
# this is how you read it
a = True
b = False
c = False
if (a or b) and c:
print('yes')
else:
print('no')
###
# this is how python reads it
a = True
b = False
c = False
if a or (b and c):
print('yes')
else:
print('no')
It is how the data is loaded into the interpreter. https://www.tutorialspoint.com/logic-gates-in-python
+ 2
Great question, it has to do with Operation Precedence, think of it like an extended Mathematic Order of Operations. AND operations have a higher priority and are evaluated first, then the OR operations.
https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html