+ 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

9th Aug 2020, 7:53 PM
Qui-Gon Jinn
Qui-Gon Jinn - avatar
6 Respostas
+ 10
operator priority. first: b and c(=false), then a or false = true
9th Aug 2020, 8:02 PM
Julia Shabanova
Julia Shabanova - avatar
+ 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?
9th Aug 2020, 8:11 PM
Qui-Gon Jinn
Qui-Gon Jinn - avatar
+ 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
9th Aug 2020, 8:04 PM
Steven M
Steven M - avatar
+ 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
9th Aug 2020, 8:28 PM
Steven M
Steven M - avatar