True, False and Comparisons

A comparison such as height >= 48 asks Python a yes-or-no question, and its answer is a Boolean value, True or False, the value every decision a program makes is built on.

Novice · Python · Updated

By

After this lesson you can

  • Compare two values with ==, !=, <, >, <= and >= and tell whether the answer is True or False
  • Tell = (gets) apart from == (is equal to), and compare text exactly, capital letters included

Key takeaways

  • True and False are Python's two Boolean values, written with a capital letter and no quotes. A comparison always answers with one of them.
  • The comparison operators are ==, !=, <, >, <= and >=. Read them aloud as "is equal to", "is not equal to", "is less than" and so on.
  • The operators with an equals sign include the edge, so 48 >= 48 is True while 48 > 48 is False.
  • A single = stores a value (read "gets"), and a double == asks whether two values are equal.
  • Text compares exactly, capital letters included, and text never equals a number, so turn a typed answer into a number before comparing it.

Every decision a program makes starts with a question that has a yes-or-no answer. Is the password right? Is the cart over $50, so shipping is free? Has the timer reached zero? Before a program can choose what to do, it has to ask, and this lesson shows how Python asks and what it answers.

This chapter's example is Splash Valley Water Park: ticket prices that depend on a guest's age and the day, a height check at the Big Splash slide, and a family's tickets added up at the gate.

A question with two possible answers

The Big Splash slide has a sign at its entrance: riders must be at least 48 inches tall. A ride attendant stands each guest against the sign and gets one of two answers: tall enough, or not yet. The park's new gate scanner has to ask the same question for every guest.

In Python, you ask it with a comparison:

height = 50
print(height >= 48)

height = 45
print(height >= 48)
True
False

>= is read "is greater than or equal to", so the line asks "is height greater than or equal to 48?" The answer is one of two values, True or False. A value that can only be true or false is called a Boolean, and Python's name for its type is bool:

print(type(True))
print(type(50 >= 48))
<class 'bool'>
<class 'bool'>

Write the two values exactly as Python does: a capital letter and no quotes. "True" in quotes is text, and a lowercase true is a name Python doesn't know: it stops with NameError: name 'true' is not defined. Did you mean: 'True'?

A comparison's answer is a value like any other, so a variable can keep it:

height = 52
tall_enough = height >= 48
print("Tall enough for Big Splash:", tall_enough)
Tall enough for Big Splash: True

A name such as tall_enough reads like the question it answers.

Six ways to compare

Python has six comparison operators. Each one asks a question about two values and answers True or False. Here they are with height = 50:

Operator Read aloud Example Answer
== is equal to height == 48 False
!= is not equal to height != 48 True
< is less than height < 48 False
> is greater than height > 48 True
<= is less than or equal to height <= 50 True
>= is greater than or equal to height >= 50 True

The operators with an equals sign include the edge. A guest exactly 48 inches tall passes height >= 48 but not height > 48. The edge is where most comparison mistakes hide, so turn the sign's words into the operator carefully: "at least 48" is >= 48, "over 48" is > 48, and "under 3" is < 3.

The ticket booth asks its own questions about age:

age = 12
print("Under 3, so free:", age < 3)
print("12 or younger, so the child price:", age <= 12)
print("Exactly 12:", age == 12)
print("Not 65:", age != 65)
Under 3, so free: False
12 or younger, so the child price: True
Exactly 12: True
Not 65: True

One equals sign or two

= and == look alike and do different jobs. One equals sign stores a value: tickets = 4 is read "tickets gets 4", and it changes the variable. Two equals signs ask a question: tickets == 4 is read "tickets is equal to 4?", and it changes nothing.

tickets = 4
print(tickets == 4)
print(tickets == 5)
print(tickets)
True
False
4

Only the first line changes tickets; the next two ask, so it's still 4 at the end. Read them aloud as "gets" and "is equal to", and the two never blur. Put one = where a question belongs, as in the next lesson's if tickets = 4:, and Python stops before running anything: SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

Comparing text

== works on text too, and it's exact: the same letters, in the same order, with the same capital letters.

day = "Saturday"
print(day == "Saturday")
print(day == "saturday")
print(day.lower() == "saturday")
True
False
True

A guest who types saturday didn't mean a different day. Compare day.lower(), the all-lowercase copy from Strings and f-strings, and every way of typing it matches.

Text and numbers never match, even when they look the same. "50" in quotes is text, so it isn't equal to the number 50, and asking whether "50" >= 48 stops the program with TypeError: '>=' not supported between instances of 'str' and 'int'. This catches people with input(), which always gives text: turn the answer into a number with int() first, as Asking for Input, and Converting Types shows.

typed = "50"
print(typed == 50)
print(int(typed) == 50)
False
True

Do it: answer the gate scanner's three questions

Splash Valley's gate scanner measures each guest and lights up three signs: the Big Splash slide, the Minnow Pool for small children, and the Wave Pool's deep end.

  • height: the guest's height in inches.

The practice box can't wait for someone to type, so the starter gives you a function line, def show_signs(height):, and the tests call it with several heights. Functions come in Module 2 (What a Function Is: def and Calling); for now, write your lines indented under that line, and use height as a variable that already holds one guest's height.

Your job: inside show_signs(height), print three lines, in this order, each with a label and True or False:

  • Big Splash: then whether the guest is at least 48 inches tall.
  • Minnow Pool: then whether the guest is under 42 inches tall.
  • Deep end: then whether the guest is over 54 inches tall.

Worked through: with show_signs(48), 48 is at least 48 (True), 48 isn't under 42 (False), and 48 isn't over 54 (False), so it prints these three lines:

Big Splash: True
Minnow Pool: False
Deep end: False

Practiced live on the lesson page: write the Python right there and check it.

Check yourself

2 questions to try on the lesson page.

So what

Every if, every loop and every test you'll write asks a comparison first, and now you know how to ask one and what Python answers.

Key terms

Related