Boolean Expressions
Table of contents
Description
A boolean expression is any logical statement that evaluates to either True
or False
. They can be a comparison, an identity or membership test, or methods that return a boolean value.
- Python Docs: Truth Value Testing
Example
Comparison Operators
<
, <=
, >
, >=
, ==
, and !=
are comparison operators. They compare 2 values and return a True
or False
. They can also be “chained” together:
name > "a"
17 < age < 26
Boolean Operators
and
, or
, and not
are boolean operators. They are used to change or combine the results of a boolean expression:
age < 18 or age > 65
age > 17 and age < 26
Boolean Functions and Methods
Methods and functions applied to variables can sometimes return a True
or False
:
name.islower()
name.isalpha()