is_open = True
is_closed = False
type(is_open) # <class 'bool'>
Two values. Lowercase true/false won't work — Python is fussy about the capitals.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 5 | False |
> | Greater than | 10 > 3 | True |
< | Less than | 10 < 3 | False |
>= | Greater or equal | 5 >= 5 | True |
<= | Less or equal | 5 <= 4 | False |
= stores a value. == tests for equality. x = 5 is "store 5 in x". x == 5 is "is x equal to 5?". Mixing them up is the most common beginner bug.
age = 25
income = 80000
(age > 18) and (income > 50000) # True
(age < 18) or (income > 100000) # False
not (age < 18) # True
Python treats some non-boolean values as if they were True or False:
0, 0.0, "" (empty string), [] (empty list), None.name = ""
if name:
print(f"Hi, {name}")
else:
print("No name provided.")
# Output: No name provided.
in operator — bonus useful"alice" in ["alice", "bob", "carol"] # True
"@" in "alice@example.com" # True
Often replaces a chunky IF + OR formula in Excel.
True and False with a capital letter.== compares; = assigns. Don't mix them up.and, or, not."", [], 0, None) count as False.What does each of these print?
print(10 == 10.0)
print(10 == "10")
print("a" in "alpha")
print(0 or 5)
True — Python is happy comparing 10 (int) and 10.0 (float).False — a number is never equal to a string of that number.True — "a" appears in "alpha".5 — or returns the first truthy value.