After this lesson you can ask Python yes-or-no questions about values. Those answers drive every decision a program makes, from checking a password length to deciding whether a book is overdue.
The two boolean values
A bool is the simplest type Python has. It has exactly two values, True and False. Both are written with a capital first letter and without quotes. Written as "True" with quotes, it would be an ordinary string.
is_open = True
is_closed = False
print(is_open, type(is_open))
print(is_closed)True <class 'bool'>
FalseA boolean is rarely typed by hand. Most of the time it is produced by a comparison.
The six comparison operators
A comparison looks at two values and answers with a bool.
stock = 12
print(stock == 12)
print(stock != 12)
print(stock > 10)
print(stock < 10)
print(stock >= 12)
print(stock <= 11)True
False
True
False
True
FalseIn order: equal to, not equal to, greater than, less than, greater than or equal to, and less than or equal to. Reading the results back, only three of the six are True. Make a habit of predicting each answer before you run the code. It is one of the best ways to learn.
The classic mix-up: = against ==
A single equals sign is assignment. It stores a value in a name. A double equals sign is a comparison. It asks a question. Python protects you from the worst of the mix-up, because using = where a comparison is needed is a syntax error rather than a silent bug.
age = 18
print(age == 18)TrueRead the first line as "age becomes 18" and the second as "is age equal to 18?".
Comparing different kinds of value
Whole numbers and decimals compare by their value, so 3 == 3.0 is True. Text and numbers are never equal, though: "3" == 3 is False, because they are different types.
print(3 == 3.0)
print("3" == 3)True
FalseThis is why the conversions from the previous lesson matter. A value typed in as text must be converted before you compare it with a number. Ordering comparisons such as < between text and a number raise a TypeError.
print("10" < 5)TypeError: '<' not supported between instances of 'str' and 'int'Comparing strings
Strings are compared one character at a time, from the left, using each character's underlying code number. Two things follow. Equality is exact, so "Book" == "book" is False. And every uppercase letter sorts before every lowercase one.
print("Book" == "book")
print("apple" < "banana")
print("Zebra" < "apple")
print("cat" < "catalogue")False
True
True
TrueThe last line is True because a shorter string that matches the start of a longer one counts as smaller. To make a fair, case-insensitive comparison, change both sides to the same case first. The next section of the course covers the string methods that do that.
Chained comparisons
Python lets you chain comparisons the way you would write them in maths. 0 <= score <= 100 asks whether the score is between 0 and 100, inclusive, in a single expression.
score = 85
print(0 <= score <= 100)
print(90 <= score <= 100)True
FalseMost other languages need two comparisons joined together for this. Python's chained form is shorter and easier to read.
Comparing the identity of objects
There is one more pair of comparison keywords, is and is not. They ask whether two names point at the very same object in memory, not merely whether the values match. For now, use == to compare values, and keep is for the special value None, which a later lesson explains.
Booleans are ordinary values
A comparison produces a normal value that you can store, print, pass around and combine like any other. Saving the result under a good name often makes code easier to read than repeating the comparison. For example, is_overdue = days_out > limit tells a reader what the program is thinking, and later lines can simply use is_overdue. Names that start with is_, has_ or can_ are a common habit for booleans because they read naturally in a sentence. You will use these stored answers constantly in the next section, when if statements and loops decide what to do based on them.
Common mistakes
- Using one equals sign in a comparison.
if x = 5is a syntax error. Use==. - Writing
trueorTRUE. OnlyTrueandFalsewith exactly that capitalisation work. - Comparing text with a number.
"5" == 5is alwaysFalse. Convert first. - Forgetting case.
"Yes" == "yes"isFalse. - Using
isto compare numbers or text. It may seem to work for small cases but it is not meant for that. Use==.
A video version of this topic is in the video course at /learn/python/.
Exercise
A library lends a book for 14 days. Store days_out = 16 and limit = 14. Print whether the book is overdue, whether it was returned exactly on the limit, and whether days_out is between 10 and 20 inclusive using one chained comparison.
Show a possible answer
days_out = 16
limit = 14
print(days_out > limit)
print(days_out == limit)
print(10 <= days_out <= 20)True
False
TrueKey points
- A bool is either True or False, written with a capital first letter and no quotes.
- The comparison operators are ==, !=, <, >, <= and >=. Each one produces a bool.
- A single = assigns a value. A double == compares two values.
- Strings compare letter by letter, and uppercase letters sort before lowercase ones.
- Python lets you chain comparisons, so 0 < x < 10 reads like it does in maths.