PART 1 · PYTHON FROM ZERO · LESSON 5
True, False and comparisons
FreeLesson 7 of 78
Every decision a program makes comes down to one question with a True or False answer.
amount = 1198
print(amount > 1000, amount < 500)
print(amount == 1198, amount != 1198) # == compares, = assigns
print(amount >= 1198, amount <= 1000)
print(500 < amount < 2000) # chained: between
status = "delivered"
print(status == "Delivered") # capitals matter
print(status.lower() == "delivered")Output
True False True False True False True False True
amount, status = 1198, "delivered"
print(amount > 1000 and status == "delivered") # both must be True
print(amount > 5000 or status == "delivered") # at least one
print(not status == "cancelled") # flips itOutput
True True True
| A | B | A and B | A or B |
|---|---|---|---|
| True | True | True | True |
| True | False | False | True |
| False | False | False | False |
for value in [0, 15, "", "Pune", [], [1, 2], None]:
print(repr(value), "->", bool(value))Output
0 -> False 15 -> True '' -> False 'Pune' -> True [] -> False [1, 2] -> True None -> False
| Write | Means |
|---|---|
x == y x != y | Equal / not equal |
x > y x < y x >= y x <= y | Greater, less, or equal to |
a and b | True only if both are True |
a or b | True if at least one is True |
not a | The opposite |
x in [1, 2, 3] | True if x is one of the items |
x is None | The correct way to test for "no value" |
Try it yourself before moving on
- Write one line that is True when 1499 is between 1000 and 2000.
- Is the empty string "" truthy or falsy? Check with bool().
- Write a condition that is True only for delivered orders above 1,000.