Введение в Python
Теория: Логические операторы
Полный доступ к материалам
Logical expressions in Python
Logical expressions
- occur when use compare or boolean operators
>,<,==,is,in,and,or,not- are used in if operator
- return boolean values
Examples
Boolean operators
- AND (binary) — true if both operands are true
- OR (binary) — true if at least one of operands is true
- NOT (unary) — reverse
NOT has higher priority than AND and OR
Examples
Any object behaves like bool
How to override bool behaviour
- Override
__nonzero__method in Py2 __bool__in Py3
How to override bool behaviour
Expressions return values
Is value None?
Ternary operator
Boolean expressions are lazy
So put the easiest expressions in OR at the first place.
Conclusion
- occur when use compare or logical operators
- return boolean value
- any object can be used as a predicate
value is None, but notvalue == None- use ternary operator
- remember about laziness