4 ms·
I think the sentence in the introduction, "Beginners construct valid Python expressions that don’t do what they seem like they should do.", is meant to include
by eesmith 9y ago
I think the sentence in the introduction, "Beginners construct valid Python expressions that don’t do what they seem like they should do.", is meant to include people who aren't coming from any programming language.
- tome 9y agoYes, moreover, it should be a type error.
- eesmith 9y agoIt isn't a type error: % python Python 3.6.0 ... >>> answer = "yes" >>> if answer == "y" or "yes": ... print("Thanks") ... Thanks The expression is the same as: (answer == "y") or "yes" If answer is 'y' then the if-expression evaluates to True; for any other string it evaluates to "yes": >>> "y" == "y" or "yes" True >>> "yes" == "y" or "yes" 'yes' >>> "no" == "y" or "yes" 'yes' Both True and 'yes' are considered True for an if-expression.
- deleted 9y ago[deleted]
- elcapitan 9y agoSo the use case where you actually want that exact syntax is "if a == someValue or someVariableIsNotNull"
- dllthomas 9y agoI think you read the parent as saying "I expect that Python currently reports this as a type error", which of course is not the case. I suspect, however, that the parent intended something more along the lines of "Other things being equal, the better language would reject this as a type error."