Booleans and comparisons in Python
True and False, and the expressions that produce them.
age = 34
has_ticket = True
is_adult = age >= 18
can_enter = is_adult and has_ticket
needs_help = not is_adult or age > 65
empty = bool("")
full = bool("text")
zero = bool(0)
in_range = 0 <= age < 120
How it works
- Comparison operators return booleans directly.
and,or,notcombine simple checks into rules.boolshows truthiness: empty things are falsy.- Comparisons chain:
0 <= age < 120reads like math.
Keywords and builtins used here
bool
The run, in numbers
- Lines
- 12
- Characters to type
- 202
- Tokens
- 54
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 40 seconds.
Step 3 of 6 in Variables & types, step 3 of 72 in Language basics.