Himanshu SharmaSoftware engineer
Let’s talk

Python Tutorial · Lesson 12 of 70

Python booleans and comparison operators

Learn True and False, the six comparison operators, how to compare strings and chained comparisons, and why = and == are different in Python.

11 minData types and operatorsUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonWrite comparisons that produce True or False, and avoid mixing up assignment with equality.

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.

python
is_open = True
is_closed = False
print(is_open, type(is_open))
print(is_closed)
result
True <class 'bool'>
False

A 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.

python
stock = 12
print(stock == 12)
print(stock != 12)
print(stock > 10)
print(stock < 10)
print(stock >= 12)
print(stock <= 11)
result
True
False
True
False
True
False

In 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.

python
age = 18
print(age == 18)
result
True

Read 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.

python
print(3 == 3.0)
print("3" == 3)
result
True
False

This 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.

python
print("10" < 5)
result
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.

python
print("Book" == "book")
print("apple" < "banana")
print("Zebra" < "apple")
print("cat" < "catalogue")
result
False
True
True
True

The 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.

python
score = 85
print(0 <= score <= 100)
print(90 <= score <= 100)
result
True
False

Most 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 = 5 is a syntax error. Use ==.
  • Writing true or TRUE. Only True and False with exactly that capitalisation work.
  • Comparing text with a number. "5" == 5 is always False. Convert first.
  • Forgetting case. "Yes" == "yes" is False.
  • Using is to 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
python
days_out = 16
limit = 14
print(days_out > limit)
print(days_out == limit)
print(10 <= days_out <= 20)
result
True
False
True

Key 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.

Check yourself

1. Which operator checks whether two values are different?
2. What is the result of 7 >= 7?
3. What does age = 18 do, compared with age == 18?
4. What is 'Zebra' < 'apple'?