True, False, and how Python compares things
Before Python can make a decision, it needs to evaluate whether something is true or false. That's where booleans come in.
The boolean type
A boolean is a value that is either True or False — nothing else:
Python
Note the capital letters. True and False are keywords in Python — true and false (lowercase) will throw an error.
Comparison operators
Most of the time you won't write True or False directly — you'll get them as the result of a comparison:
Python
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
= vs ==
This trips up almost everyone at least once:
Python
= is assignment. == is a question: "are these the same?" They are not interchangeable.
Combining comparisons
Use and, or, and not to combine booleans:
Python
Key takeaways
- Booleans are either
TrueorFalse— capital letters required - Comparison operators return booleans — use them to ask questions about your data
=assigns a value;==compares two values — don't mix them upand,or, andnotlet you combine and invert boolean expressions
No comments yet. Add the first comment to start the discussion.