True and False — learning to ask questions
Comparison operators, and / or / not, chained comparisons, and which values Python treats as false — the groundwork for writing conditions.
- 1Encounter
- 2Understand
- 3Worked
- 4Predict
- 5Apply
- 6Stretch
The problem we are solving
A program has to decide something: does this order qualify for a discount? Is the password long enough? Is the item in stock?
The code that decides comes in the next mission (if). But before that you need the thing a decision rests on — a question whose answer is yes or no. In Python those answers are called True and False, and the type is called bool.
Today we only learn to ask, not to decide. Splitting it that way is deliberate: nearly every mistake people make writing an if is really a mistake in the question, not in the if.
By the end of this mission you can
- Use the six comparison operators
- Tell
=and==apart - Join small questions into larger ones with
and,orandnot - Write chained comparisons like
18 <= age <= 60 - Say which values Python treats as false without being
False
Prerequisites: Taking input from the person using your program.
The six comparisons
age = 22
print(age == 22)
print(age != 22)
print(age > 18)
print(age < 18)
print(age >= 22)
print(age <= 21)True
False
True
False
True
FalseEvery comparison produces a value — True or False. Which means a comparison can be stored in a variable:
age = 22
is_adult = age >= 18
print(is_adult)
print(type(is_adult))True
<class 'bool'>Naming it that way makes the code far easier to read — is_adult says what is being checked, while age >= 18 makes the reader work it out.
= and == — one character, a different meaning
total = 100 # an instruction: put 100 into total
print(total == 100) # a question: is total 100?True=stores (assignment)==asks (comparison)
Write = where a condition belongs and Python 3 raises a clear error, with a helpful message:
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?Good news — this mistake does not stay quiet, it gets caught.
Comparing text
print("rafi" == "rafi")
print("Rafi" == "rafi")
print("apple" < "banana")
print(len("password123") >= 8)True
False
True
TrueTwo things to notice:
Case matters. "Rafi" is not equal to "rafi". When matching text a person typed, run .lower() first — otherwise anyone who capitalised their name is excluded:
typed = " RAFI "
print(typed == "rafi")
print(typed.strip().lower() == "rafi")False
True< works on text too, following dictionary order — hence "apple" < "banana". That is the basis of sorting a list of names.
and, or, not
Three words join small questions into bigger ones:
age = 22
has_id = True
print(age >= 18 and has_id)
print(age >= 60 or has_id)
print(not has_id)True
True
Falseand— true when both are trueor— true when at least one is truenot— flips it
The whole picture at once:
print(True and True)
print(True and False)
print(False and False)
print(True or False)
print(False or False)True
False
False
True
Falseormeans "one or more", not "exactly one". In everyday speech "tea or coffee?" implies one of them. Python'sorisTrueeven when both sides are true. Forget that and conditions will surprise you.
not always applies to whatever comes immediately after it, and it runs first. The order is: not first, then and, and or last.
print(True or False and False)
print((True or False) and False)True
FalseOn the first line and ran first, so Python read True or (False and False) — that is True or False, which is True. On the second, the parentheses changed the order. When in doubt, use parentheses — as true here as it was for arithmetic.
Chained comparisons
To check whether something sits between two bounds, Python lets you write it the way mathematics does:
marks = 72
print(0 <= marks <= 100)
print(marks > 60 and marks < 80)
print(60 < marks < 80)True
True
TrueThe second and third lines say the same thing, and the third reads better. Many languages do not allow this; Python does, and you should use it.
Which values Python treats as false
It is not only False — a handful of ordinary values count as false too:
print(bool(0))
print(bool(1))
print(bool(-5))
print(bool(""))
print(bool("hello"))
print(bool("0"))
print(bool("False"))False
True
True
False
True
True
TrueThe rule in one line: zero and empty things are false, everything else is true. So 0 is false and empty text "" is false; but -5 is true (it is not zero), and "0" and "False" are true — because they are not empty, they contain characters.
Those last two lines are the real trap. Everything from input() is text, so even when someone types 0, you hold "0" — which Python considers true. To compare numbers you must convert with int() first.
The "empty is false" rule has a practical use as well — catching someone who pressed Enter without typing:
name = input("Name: ").strip()
print("Name was given:", bool(name))Name:
Name was given: FalseA complete example
eligibility.py:
# Check a discount rule without deciding anything yet
age = int(input("Age : "))
years_member = int(input("Years a member : "))
order_total = float(input("Order total : "))
is_adult = age >= 18
is_senior = age >= 60
is_loyal = years_member >= 2
is_large_order = order_total >= 1000
# The rule: adults who are either long-standing members or spending a lot
gets_discount = is_adult and (is_loyal or is_large_order)
print()
print(f"Adult : {is_adult}")
print(f"Senior : {is_senior}")
print(f"Loyal member : {is_loyal}")
print(f"Large order : {is_large_order}")
print(f"Gets discount : {gets_discount}")Age : 34
Years a member : 1
Order total : 1500
Adult : True
Senior : False
Loyal member : False
Large order : True
Gets discount : TrueNotice that each condition was given its own name first, and the rule was then assembled from them. Written as one line — age >= 18 and (years_member >= 2 or order_total >= 1000) — the code would run identically, but reading it six months later to recover the rule would be hard work. The parentheses matter too: without them and would run first and the rule itself would change.
When it breaks
SyntaxError: invalid syntax. Maybe you meant '==' ... A = was written where a comparison belongs. Use ==.
These should be equal, but I get False Check three things: stray spaces (.strip()), letter case (.lower()), and type — "22" == 22 is always False, because text and a number are never equal.
TypeError: '<' not supported between instances of 'str' and 'int' You compared text with a number, probably having forgotten to convert an input(). Wrap it in int().
Two decimals will not compare equal with == Expected — recall the decimal trap from the arithmetic mission. Compare after round().
The condition does not do what it says Check and/or order: not first, then and, then or. Use parentheses to state your intent explicitly.
Step 4 of 6 — Predict
Check your understanding
What is the output of these three lines?
print(5 > 3 and 2 > 4)
print(5 > 3 or 2 > 4)
print(not 5 > 3)- AFalse True False
- BTrue True False
- CFalse False True
- DFalse True True
How does each of these three values count as true or false?
print(bool("False"))
print(bool(""))
print(bool(0))- ATrue False False
- BFalse False False
- CFalse True False
- DTrue True False
The person typed Yes (capital Y, one trailing space) and the program says Continuing: False. Why, and what is the best fix?
answer = input("Type yes to continue: ")
print("Continuing:", answer == "yes")- ABoth the trailing space and the capital letter count in a comparison — it needs `answer.strip().lower() == "yes"`
- BThe `==` should be a `=`
- CAn `input()` can never equal `"yes"`, so `in` has to be used instead
- D`answer` has to be converted with `bool()` first
Answering needs an account
Sign in to check your answers
The questions are above, and working them out in your head is the part that matters. Sign in to see the answers, the explanations and the three-level hints.
Your turn
Write a file called password_check.py that takes a password and prints True/False for four questions:
- Length is at least 8
- Length is between 8 and 20 (write this as a chained comparison)
- The word
"password"does not appear in it (you will need bothnotandin, and you should lowercase before comparing) - Whether all three above are true at once
Then run it with at least four different passwords: one very short, one very long, one containing Password with a capital letter, and one that satisfies everything. Check that all four answer correctly — especially the third, because that is where the letter-case trap is hiding.
There is more in this mission
What you read above is the complete mission — nothing was cut out of it. Pro adds:
- What actually happens underneath — how Python runs this
- The full solution to the exercise, worked step by step
- Harder extra exercises
- The interview question on this topic, with a model answer
Step 6 of 6
Stretch — the chapter quiz
Ten questions from easy to hard. The last ones are difficult on purpose.
Sign in to take the quiz