Booleans and comparisons
Before a program can make a choice, it needs to ask a yes-or-no question: is it dark yet? Is the lamp oil low? Python answers such questions with one of two values, True or False.
1oil = 32print(oil < 5)3print(oil == 10)
True False
True and False
True and False are a type of their own, called bool (short for *boolean*). You can store them in variables like any other value. They start with a capital letter and have no quotes.
1is_dark = True2print(is_dark)3print(type(is_dark))4print(type("True"))
True <class 'bool'> <class 'str'>
"True" in quotes is just a four-letter string. And true with a small t is a name Python doesn't know:
1lamp_on = trueTraceback (most recent call last):
File "main.py", line 1, in <module>
lamp_on = true
NameError: name 'true' is not defined. Did you mean: 'True'?Comparison operators
A comparison takes two values and gives back True or False.
| Operator | Means | Example | Result |
|---|---|---|---|
== | equal to | 3 == 3 | True |
!= | not equal to | 3 != 3 | False |
< | less than | 2 < 5 | True |
> | greater than | 2 > 5 | False |
<= | less than or equal to | 5 <= 5 | True |
>= | greater than or equal to | 4 >= 5 | False |
1houses = 122lamps = 93print(lamps < houses)4print(lamps >= houses)5print(houses != 12)6print(houses - lamps == 3)
True False False True
The last line shows that the math happens first, then the comparison: 12 - 9 is 3, and 3 == 3 is True.
Tip: One = stores a value (x = 5). Two, ==, ask a question (x == 5). Mixing them up is the most common beginner slip.
Numbers compare by value, so int and float mix freely: 5 == 5.0 is True.
Chained comparisons
To check that a value lies between two others, write it the way you would on paper. Python reads 0 < oil < 10 as "0 < oil and oil < 10".
1oil = 72print(0 < oil < 10)3print(1 <= oil <= 5)4print(10 < oil < 0)
True False False
Many other languages make you write the two halves separately. In Python the chain is the usual way.
Comparing strings
== on strings checks that they are exactly the same, character by character. Capitals count, and so do spaces.
1print("lamp" == "lamp")2print("Lamp" == "lamp")3print("lamp " == "lamp")4print("Lamp".lower() == "lamp")
True False False True
< and > compare strings like a dictionary does, letter by letter from the left. But every capital letter comes before every small one, so "Zebra" < "apple".
What does this print?
1print("10" < "9") These are strings, not numbers, so Python compares the first characters: "1" comes before "9". It never looks at the whole number. Convert input with int() before comparing amounts.
A string and a number are never equal, and Python refuses to put them in order:
1print("5" == 5)2print("5" < 5)
False
Traceback (most recent call last):
File "main.py", line 2, in <module>
print("5" < 5)
TypeError: '<' not supported between instances of 'str' and 'int'and, or, not
These three words combine True and False values:
| Expression | Is True when |
|---|---|
a and b | both a and b are true |
a or b | at least one of them is true |
not a | a is false |
1is_dark = True2has_oil = False3print(is_dark and has_oil)4print(is_dark or has_oil)5print(not has_oil)6print(is_dark and not has_oil)
False True True True
When you mix them, not is worked out first, then and, then or. Parentheses make the order clear to people as well:
1rain = True2wind = False3night = True4print(rain or wind and night)5print((rain or wind) and night)6print(not rain or night)
True True True
Truthy and falsy values
Every value, not only True and False, counts as true or false when Python needs a yes or a no. bool() shows which. The rule is short: zero and empty are false, and almost everything else is true.
1print(bool(0), bool(0.0), bool(""), bool(None))2print(bool(42), bool(-1), bool("hi"), bool(" "))3print(bool("0"), bool("False"))
False False False False True True True True True True
The last line surprises people: "0" and "False" are strings with characters in them, so they count as true. Lesson 8 uses this to write short checks like "if the user typed anything".
Methods that answer a question also give back a bool, so you can combine them with and, or and not:
What does this print?
1text = "42"2print(text.isdigit() and "4" in text)
text.isdigit() is True, and "4" in text is True too. Both sides are true, so and gives True.
Practice
Write the code, then press Check. A hint costs nothing; the answer is there when you need it. Each exercise prints one bool: replace the ... with an expression.
Enough oil?
✓A lamp needs at least 4 units of oil. Print True if oil is enough, otherwise False.
True
1oil = 42print(...)
Opening hours
✓The shop is open from 9 up to, but not including, 17. Print whether hour is within opening hours, using one chained comparison.
False
1hour = 172print(...)
Can Wick go out?
✓Wick goes out when it is dark and it is not raining. Print the result for the values below.
False
1is_dark = True2is_raining = True3print(...)
What you learned
TrueandFalseare the twoboolvalues: capital first letter, no quotes.==,!=,<,>,<=,>=compare two values and give a bool.=stores,==asks.- Chains like
0 < x < 10check a range in one go. - Strings compare exactly and letter by letter: capitals matter, and
"10" < "9". and,or,notcombine bools;notgoes first, thenand, thenor.- Zero, empty strings and
Nonecount as false; almost everything else counts as true.