Kodewick
Lesson 7 of 31Contents

Booleans and comparisons

Lesson 7 of 31 · Part 1 · Basics · about 15 minutes

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.

main.py
1oil = 32print(oil < 5)3print(oil == 10)
Output
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.

main.py
1is_dark = True2print(is_dark)3print(type(is_dark))4print(type("True"))
Output
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:

main.py
1lamp_on = true
Output
Traceback (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.

OperatorMeansExampleResult
==equal to3 == 3True
!=not equal to3 != 3False
<less than2 < 5True
>greater than2 > 5False
<=less than or equal to5 <= 5True
>=greater than or equal to4 >= 5False
main.py
1houses = 122lamps = 93print(lamps < houses)4print(lamps >= houses)5print(houses != 12)6print(houses - lamps == 3)
Output
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".

main.py
1oil = 72print(0 < oil < 10)3print(1 <= oil <= 5)4print(10 < oil < 0)
Output
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.

main.py
1print("lamp" == "lamp")2print("Lamp" == "lamp")3print("lamp " == "lamp")4print("Lamp".lower() == "lamp")
Output
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".

Guess first

What does this print?

1print("10" < "9")

A string and a number are never equal, and Python refuses to put them in order:

main.py
1print("5" == 5)2print("5" < 5)
Output
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:

ExpressionIs True when
a and bboth a and b are true
a or bat least one of them is true
not aa is false
main.py
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)
Output
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:

main.py
1rain = True2wind = False3night = True4print(rain or wind and night)5print((rain or wind) and night)6print(not rain or night)
Output
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.

main.py
1print(bool(0), bool(0.0), bool(""), bool(None))2print(bool(42), bool(-1), bool("hi"), bool(" "))3print(bool("0"), bool("False"))
Output
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:

Guess first

What does this print?

1text = "42"2print(text.isdigit() and "4" in text)

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.

Exercise 1

Enough oil?

A lamp needs at least 4 units of oil. Print True if oil is enough, otherwise False.

It should print
True
exercise1.py
1oil = 42print(...)
Output
Press Run or Ctrl+Enter
Exercise 2

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.

It should print
False
exercise2.py
1hour = 172print(...)
Output
Press Run or Ctrl+Enter
Exercise 3

Can Wick go out?

Wick goes out when it is dark and it is not raining. Print the result for the values below.

It should print
False
exercise3.py
1is_dark = True2is_raining = True3print(...)
Output
Press Run or Ctrl+Enter

What you learned

  • True and False are the two bool values: capital first letter, no quotes.
  • ==, !=, <, >, <=, >= compare two values and give a bool. = stores, == asks.
  • Chains like 0 < x < 10 check a range in one go.
  • Strings compare exactly and letter by letter: capitals matter, and "10" < "9".
  • and, or, not combine bools; not goes first, then and, then or.
  • Zero, empty strings and None count as false; almost everything else counts as true.