Python Booleans
🐍 Python Booleans (True / False)
Booleans represent truth values in Python.
There are only two Boolean values:
⚠ Note: The first letter must be capital (True, not true).
✅ Boolean Type
You can check the data type using type():
🧐 Boolean from Comparison
Booleans are commonly returned from comparison operators:
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater or equal |
<= | Less or equal |
Example:
🧠 Boolean in if Condition
Booleans control program decisions:
Output:
🔄 Boolean from Values using bool()
Python automatically converts values to booleans:
❗ Values that become False:
| Value | Result |
|---|---|
0 | False |
0.0 | False |
None | False |
"" (empty string) | False |
[] (empty list) | False |
{} (empty dict) | False |
() (empty tuple) | False |
Everything else becomes True.
🔧 Boolean Operators
Python uses:
| Operator | Meaning |
|---|---|
and | Both must be True |
or | At least one must be True |
not | Reverses result |
Examples:
Real example:
