livepython.net
CHAPTER 05Working With Data Types
5.3  BOOLEANS

Python Booleans and Comparison Operators

True and False

A boolean (bool) has only two values: True and False. You met them in chapter 4. In this topic, you'll see where they really come from: questions your program asks.

Think of a boolean as a light switch: on or off, yes or no. Nothing in between.

PYTHONedit me
is_raining = False
has_umbrella = True
print(is_raining, has_umbrella)
print(type(is_raining))
OUTPUT
False True
<class 'bool'>

The rules, again

  • Capital first letter: True, False.
  • No quotes: "True" is a string, not a boolean.
PYTHONedit me
print(type(True))
print(type("True"))
OUTPUT
<class 'bool'>
<class 'str'>

Booleans are answers

You'll rarely type True or False yourself. Most booleans come from comparing values: "is the score above 50?", "is the password right?". Python answers each question with True or False.

PYTHONedit me
score = 72
print(score > 50)
OUTPUT
True

That > is a comparison operator. You'll learn all of them in the next lesson. In chapter 8, if will use these answers to make decisions, like the diamond in a flowchart.

A fun fact

Behind the scenes, True counts as 1 and False as 0:

PYTHONedit me
print(True + True)
OUTPUT
2

You won't need this often, but it explains some surprising results later.

Booleans are True or False: the answers to yes-or-no questions.

your turnPick the right idea

Where do most booleans come from?

Think about it, then pick one.

Comparison Operators

Comparison operators compare two values and answer with a boolean:

OperatorQuestion it asksExampleAnswer
==equal?5 == 5True
!=not equal?5 != 3True
>greater than?5 > 3True
<less than?5 < 3False
>=greater than or equal?5 >= 5True
<=less than or equal?4 <= 3False
PYTHONedit me
age = 18
print(age > 10)
print(age < 10)
print(age == 18)
print(age != 18)
OUTPUT
True
False
True
False

Think of a ticket counter

"Is the visitor at least 18?" becomes age >= 18. The answer is True or False, and later the program will let them in, or not.

Store the answer

A comparison is a value like any other, so you can store it:

PYTHONedit me
marks = 45
passed = marks >= 40
print("Passed:", passed)
OUTPUT
Passed: True

>= and <= include the edge

5 > 5 is False, because 5 isn't bigger than itself. 5 >= 5 is True, because "or equal" includes it. Off-by-one mistakes here are very common, so think about the edge every time.

Common beginner doubt: "Can I write =>?" No: the = always comes second: >= and <=.

Comparisons ask a question and answer True or False.

your turnPredict the output

What will this print?

marks = 40 print(marks > 40)
Your turn! What will this code show on the screen?
your turnFill the gap

Make it print True

age = 18 print(age ? 18)

It should print: True

Which piece makes this code work?

= or ==?

This is the mix-up every beginner makes at least once, so let's settle it:

SymbolNameWhat it does
=assignmentstores a value: age = 18
==comparisonasks "are these equal?": age == 18
PYTHONedit me
age = 18
print(age == 18)
print(age == 21)
OUTPUT
True
False

The first line stores 18. The print() lines ask questions about it.

A way to remember

Read = as "gets" and == as "is equal to?".

  • age = 18 → "age gets 18".
  • age == 18 → "is age equal to 18?"

One = gives a value. Two == ask a question.

What goes wrong

Using == when you meant = doesn't store anything. And using = inside a question breaks the code:

PYTHONedit me
age = 18
print(age = 18)
OUTPUT
TypeError: print() got an unexpected keyword argument 'age'

Python thought age = 18 inside print() was a parameter, like sep=. In chapter 8 you'll see that if age = 18: gives an error too. When you ask, use ==.

One = stores a value. Two == ask if two values are equal.

your turnSpot the bug

Which line breaks?

One line stops this program. Tap it.
your turnPick the right idea

Which line asks if total is 100?

Think about it, then pick one.

Comparing Text

Comparisons work on strings too. == checks that two strings are exactly the same:

PYTHONedit me
password = "sunny123"
print(password == "sunny123")
print(password == "Sunny123")
OUTPUT
True
False

Exactly means exactly

Like variable names, string comparisons are case-sensitive: "Sunny" and "sunny" are different. Spaces count too:

PYTHONedit me
print("Sunny" == "sunny")
print("Hi" == "Hi ")
OUTPUT
False
False

That extra space at the end of "Hi " makes them different. Hidden spaces are a common reason a "correct" answer is judged wrong.

A string vs a number

PYTHONedit me
print("5" == 5)
OUTPUT
False

The text "5" and the number 5 are different types, so they're never equal. You'll learn how to convert one into the other in the next topic.

Greater and less with text

< and > compare strings in alphabetical order, letter by letter:

PYTHONedit me
print("apple" < "banana")
print("Zebra" < "apple")
OUTPUT
True
True

The second one surprises people: every capital letter comes before every small letter in this order. So for alphabetical sorting, keep the case the same.

Think of a dictionary: "apple" comes before "banana", so "apple" < "banana" is True.

Text comparisons are exact: capitals, spaces and types all count.

your turnPredict the output

What will this print?

print("Python" == "python")
Your turn! What will this code show on the screen?
your turnPredict the output

What will this print?

Score = 5 score = 9 print(Score)
Your turn! What will this code show on the screen?

Practice: Booleans

Solve these to lock in what you learned in this topic. Each one runs in your browser.

  1. 40.Python Program to Compare Two NumbersEasySolve
  2. 41.Python Program to Check If Two Passwords MatchEasySolve