Loading lessons...
Logical Operators
Logical operators combine boolean values: and, or, and not.
and
True only when BOTH sides are true:
python
print(5 > 3 and 5 < 10)
### True
print(5 > 3 and 5 > 10)
### False
or
True when AT LEAST ONE side is true:
python
print(5 > 3 or 5 > 10)
### True
print(5 < 3 or 5 > 10)
### False
not
Flips a boolean:
python
print(not True)
### False
print(not (5 > 3))
### False
A real-world example
python
age = 25
has_ticket = True
if age >= 18 and has_ticket:
print("Welcome in!")
Truth table for and
| A | B | A and B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
TL;DR
andneeds both true.orneeds at least one true.notflips the value.- Use them to combine conditions.