Cohort 4Register

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.

COMPARISONS GIVE TRUE OR FALSEPYTHON 3.12
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
AND, OR, NOTPYTHON 3.12
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 it

Output

True
True
True
ABA and BA or B
TrueTrueTrueTrue
TrueFalseFalseTrue
FalseFalseFalseFalse
"TRUTHY" AND "FALSY" VALUESPYTHON 3.12
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
WriteMeans
x == y x != yEqual / not equal
x > y x < y x >= y x <= yGreater, less, or equal to
a and bTrue only if both are True
a or bTrue if at least one is True
not aThe opposite
x in [1, 2, 3]True if x is one of the items
x is NoneThe correct way to test for "no value"

Try it yourself before moving on

  1. Write one line that is True when 1499 is between 1000 and 2000.
  2. Is the empty string "" truthy or falsy? Check with bool().
  3. Write a condition that is True only for delivered orders above 1,000.