if (x >= 1) and (x <=10):
print('x is between 1 and 10 inclusive')
Boolean operators
- These receive only Booleans as operands and give a Boolean as result.
Examples
Example 1
Example 2
if not (x == ''):
print('x is not the empty string')
Example 3
if (x < 1) or (x > 10):
print('x is not between 1 and 10')
Example 4
= "you fool!"
message if "fool" not in message:
print("Message does not contain offensive language")
Short-circuit evaluations
In (A and B)
, if A is false, then so is the expression, and there is no need to evaluate B.
In (A or B)
, if A is true, then so is the expression, and there is no need to evaluate B.
Keep an eye on that!
Remember precedence order!
- Parentheses:
()
- Exponents:
**
- Multiplication, divisions and modulus:
* / // %
- Addition and subtraction:
+ -
- Comparisons:
<= < >= > == != is in
- Boolean
not
- Boolean
and
- Boolean
or