Topic 01

True, False and Comparisons

Hands-on

Before a program can decide anything, it needs questions with a yes-or-no answer. Is the total over 50? Is the name empty? Did the person type yes? A comparison is such a question. It compares two values and produces one of the two bool values from Chapter 2: True or False.

Think of a question at a border desk: is this passport valid? The officer does not answer "fairly" or "mostly". There are exactly two answers, and what happens next depends on which one it is. Every decision in a program is built from answers like that.

The Six Comparisons

Python has six comparison operators. Two equals signs ask "is equal to". An exclamation mark and an equals sign ask "is not equal to". The less-than and greater-than signs work as in school, and adding an equals sign after either one gives "less than or equal to" and "greater than or equal to". Each comparison, typed at the prompt, answers True or False.

Comparisons answer True or False
>>> 5 == 5
True
>>> 5 != 3
True
>>> 3 < 2
False
The six comparisons
OperatorSaid aloudExampleAnswer
==is equal to5 == 5True
!=is not equal to5 != 3True
<is less than3 < 2False
>is greater than7 > 2True
<=is less than or equal to4 <= 4True
>=is greater than or equal to2 >= 9False

One Equals Sign or Two

The most common typo in beginner code is using one equals sign where two were meant. One equals sign ties a name to a value; two ask a question. Inside an if, which the next topic introduces, Python stops before running anything and says so. Read the last line of the message below: invalid syntax, followed by a suggestion that you probably meant two equals signs. The row of carets underneath marks the exact part it could not read.

equals.py
x = 5
if x = 5:
    print("five")
Output
  File "/Users/you/python-course/equals.py", line 2
    if x = 5:
       ^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

A habit that prevents the slip: read two equals signs aloud as "is equal to?", with the question mark, every time you write them.

Comparing Text

Comparisons work on text too, and they are exact. The word apple in lower case is not equal to Apple with a capital A, because to Python they are different characters. When case should not matter, which is most of the time with anything a person typed, compare lower-case versions of both sides.

Text also has an order, letter by letter, which is why a subtle trap waits for numbers stored as text. The text 10 is less than the text 9, because the first characters are compared first, and the character 1 comes before the character 9. Convert numbers from text before comparing them.

Text comparisons are exact and letter by letter
>>> "apple" == "Apple"
False
>>> "apple" == "Apple".lower()
True
>>> "10" > "9"
False

A Comparison Is a Value

A comparison does not have to live inside an if. It is an expression that produces a bool, and you can give that bool a name and use it later, exactly like a number. Naming a comparison is often the clearest way to explain what it means.

Storing the answer to a question
>>> age = 20
>>> is_adult = age >= 18
>>> is_adult
True
Common Confusions
  • "One equals sign and two equals signs are interchangeable." One assigns, two compare. Mixing them up is the most common early typo, and Python's message suggests the fix.
  • ""10" > "9" compares numbers." Text compares character by character, so the text 10 comes first. Convert text to numbers before comparing.
  • "A comparison only works inside an if." It is an ordinary value, True or False, and it can be stored, printed or combined with others.
Why It Matters
  • Every decision, loop and filter in the rest of the book is built from comparisons like these.
  • Reading two equals signs aloud as "is equal to?" prevents the single most common beginner typo.

Knowledge Check

What does the expression 7 > 2 produce?

  • True, one of the two bool values Python has
  • The number 5, the difference between the two
  • The number 7, the larger of the two values
  • Nothing, until it is placed inside an if statement

What is the difference between x = 5 and x == 5?

  • They mean the same thing in every place they appear
  • The first compares, and the second assigns a value
  • The first ties x to 5; the second asks if x is 5 now
  • The second is simply an older spelling of the first

Why is "10" > "9" False?

  • Python cannot compare text, so it answers False
  • The values are equal once their quotes are removed
  • Two-digit numbers are always treated as smaller ones
  • Text compares character by character, and 1 is before 9

How do you compare two pieces of text so that capital letters do not matter?

  • Put the comparison inside an if statement first
  • Compare a lower-case copy of each side instead
  • Use three equals signs instead of the usual two
  • Compare the lengths of the two pieces of text instead

You got correct