Yesterday your programs ran straight through, top to bottom. Today they start
making choices — and every choice comes down to one question: is this True or
False?
Comparison operators
print(3 < 5)
print(3 > 5)
print(3 <= 3)
print(3 == 3) # equal to - two equals signs
print(3 != 3) # not equal to
age = 20
print(age >= 18)
print(type(age >= 18))= and == are different
= assigns a value; == asks a question. age = 18 sets the age. age == 18
checks it. Mixing them up is a rite of passage — Python catches it as a
SyntaxError in an if, which is more than most languages do for you.
Comparisons work on strings too, alphabetically by character code:
print("apple" < "banana")
print("Zoe" < "adam") # uppercase sorts before lowercase
print("apple" == "Apple")
print("apple".lower() == "Apple".lower())
# Python lets you chain comparisons, and it reads exactly as it looks.
score = 75
print(60 <= score < 90)That last line is genuinely nice: 60 <= score < 90 is one expression, and
score is evaluated once. Few languages allow it.
Combining conditions
and, or and not join conditions together. Python spells them as words,
not symbols.
age = 25
has_ticket = True
print(age >= 18 and has_ticket)
print(age >= 65 or has_ticket)
print(not has_ticket)
# and requires both; or requires at least one.
print(True and False)
print(True or False)
# Parentheses make mixed expressions readable.
is_free = (age < 12) or (age >= 65)
print(f"Free entry: {is_free}")and returns True only when both sides are true. or returns True when at
least one is. not flips whatever it is given.
Truthiness
Every Python value can be treated as a condition, not just True and False.
The rule is short: empty is false, everything else is true.
# These are all "falsy":
print(bool(0), bool(0.0), bool(""), bool([]), bool({}), bool(None))
# Everything else is "truthy":
print(bool(1), bool(-3), bool("hello"), bool(" "), bool([0]))This is why you will see if name: rather than if name != "": in real
Python. It reads as “if there is a name”, which is what you meant.
None is not False
None is Python’s “no value at all” — what a function returns when it returns
nothing. It is falsy, but it is not False, and it is not 0. Test for it
with is None, never with == None.
Short-circuiting
and and or stop as soon as the answer is settled. This is not a
micro-optimisation — it is a tool for writing safe conditions.
def loud(value):
print(" (checked)", value)
return value
print("First:")
result = loud(False) and loud(True) # second call never happens
print("Second:")
result = loud(True) or loud(False) # second call never happens
# Which is why this is safe even when the text is empty:
text = ""
print(len(text) > 0 and text[0] == "A")Password checker
Write a condition that decides whether a password is acceptable. It must be at
least 8 characters long and contain a digit. Print True or False.
any(c.isdigit() for c in password) tells you whether any character is a
digit — you will understand that syntax fully on Day 4.
password = "hunter2000"
# Your code hereShow one solution
password = "hunter2000"
long_enough = len(password) >= 8
has_digit = any(c.isdigit() for c in password)
print(long_enough and has_digit)Naming the two halves before combining them is worth the extra lines: the final condition then reads as a sentence rather than a puzzle.
What you learned
==!=<><=>=produceTrueorFalse;=assigns.- Comparisons chain:
0 <= x < 10. and,or,notcombine conditions, and they short-circuit.- Empty values are falsy; everything else is truthy.
Nonemeans “no value”; test it withis None.