3.16 Boolean Operators and
, or
and not
The conditional operators >
, <
, >=
, <=
, ==
and !=
can be used to form simple conditions such as grade
>=
60
. To form more complex conditions that combine simple conditions, use the and
, or
and not
Boolean operators.
Boolean Operator and
To ensure that two conditions are both True
before executing a control statement’s suite, use the Boolean and
operator to combine the conditions. The following code defines two variables, then tests a condition that’s True
if and only if both simple conditions are True
—if either (or both) of the simple conditions is False
, the entire and
expression is False
:
In [1]: gender = 'Female'
In [2]: age = 70
In [3]: if gender == 'Female' and age >= 65:
...: print('Senior female' ...
Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.