Learn

Short lessons with real code you can edit and run on the page.

Booleans and Comparisons in Python

Lesson 9 Python 3.14 Runs in your browser Updated
In short

A boolean is a value that is either True or False, and a comparison such as 5 > 3 produces one. The six operators that compare values, ==, !=, >, <, >= and <=, are how a program asks a yes-or-no question before it decides what to do next.

Key facts

  • True and False are capitalized. Lowercase true is an undefined name.
  • bool is a subtype of int, so True == 1 and True + True is 2.
  • bool() returns False for False, None, zero of any number type, and empty strings and collections. Almost everything else is True.
  • == compares values and is compares identity. Use is for None checks, not to compare values.

What is a boolean in Python?

A boolean is one of two values, True or False. Its type is bool, and a comparison such as 5 > 3 produces one without you typing True or False.

Python
is_open = True
is_full = False
print(is_open)
print(type(is_open))
print(5 > 3)
print(type(5 > 3))
Output
True
<class 'bool'>
True
<class 'bool'>

What are the 6 comparison operators in Python?

Equal, not equal, greater than, less than, greater than or equal, and less than or equal. Each one answers True or False when the two values can be compared, and the Python language reference also counts is, is not, in and not in as comparison operators.

Python comparison operators
OperatorMeaningExampleResult
==Equal to7 == 7True
!=Not equal to7 != 7False
>Greater than7 > 7False
<Less than7 < 7False
>=Greater than or equal to7 >= 7True
<=Less than or equal to7 <= 7True
Python
x = 7
print(x == 7)
print(x != 7)
print(x > 7)
print(x < 7)
print(x >= 7)
print(x <= 7)
Output
True
False
False
False
True
True

Two equals signs compare. One equals sign assigns. Python has no ===, the strict equality operator of JavaScript and PHP, and Python's == never turns a string into a number the way JavaScript's == does.

Is True equal to 1 in Python?

Yes. bool is a subtype of int, with True worth 1 and False worth 0. That is why you can add booleans to count how many conditions held.

Python
print(True == 1)
print(False == 0)
print(True + True)
print(isinstance(True, int))
Output
True
True
2
True

How does bool() work in Python?

bool() converts any value to True or False. False, None, zero and empty values count as False, and almost everything else counts as True. Values that count as True are called truthy, and values that count as False are called falsy. Python applies the same rule whenever a value is used as a condition, as in the if statement of the next lesson.

Python values that count as False and as True
Counts as FalseCounts as True
False, None, 0, 0.0, "", [], (), {}, set()Almost every other value, including "0", " " and [0]
Python
print(bool(0), bool(42))
print(bool(""), bool("hi"))
print(bool([]), bool([1, 2]))
print(bool(None))
Output
False True
False True
False True
False

What is the difference between == and is?

== asks whether two values are equal, and is asks whether they are the same object in memory. Two separate lists with the same contents are equal but not identical. Beginners mostly need is for one job, checking for None, because there is only ever one None object.

Python
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)
nothing = None
print(nothing is None)
Output
True
False
True

Can I chain comparisons and compare strings?

Yes to both. 18 <= age < 65 means 18 <= age and age < 65, so both comparisons must be true. Strings compare one character at a time by each character's number in Unicode, the standard that numbers every character. That gives alphabetical order for unaccented letters of the same case and puts capitals first.

Python
age = 25
print(18 <= age < 65)
print("apple" < "banana")
print("Zebra" < "apple")
print("abc" == "ABC")
Output
True
True
True
False

"Zebra" < "apple" is True because the capitals A to Z come before the lowercase a to z in the Unicode table. Compare lower() versions when case shouldn't matter.

Common mistakes with booleans in Python

Two of these boolean mistakes come down to a single character. One is a missing = and the other a lowercase t, which is why they hide so well.

  • SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? A single = in a condition. Python 3.10 and later suggest ==, which is the fix. The other suggestion, :=, stores a value instead of comparing, so the condition would pass whenever that value counts as True.
  • NameError: name 'true' is not defined. Did you mean: 'True'? The boolean is True with a capital T.
  • TypeError: '<' not supported between instances of 'str' and 'int'. Text from input() compared with a number. Convert with int() first.
  • Comparing with == True in a condition. For a real boolean, if x: does the same job and is the usual style. For other values the two can differ. if 2: runs, but 2 == True is False, while 1 == True and 1.0 == True are both True.

An if line runs the indented line under it only when its condition counts as True, and the next lesson covers it in full. Run both examples to see the errors.

Python
x = 5
if x = 5:
    print("five")
Output
  File "main.py", line 2
    if x = 5:
       ^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
Python
print("10" < 9)
Output
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    print("10" < 9)
          ^^^^^^^^
TypeError: '<' not supported between instances of 'str' and 'int'

Exercise

Complete the two print lines. The first prints whether age is at least 18, which is False. The second uses one chained comparison to print whether age is between 13 and 19 inclusive, which is True.

Python
age = 17

print()
print()
Output
Show the solution
age = 17

print(age >= 18)
print(13 <= age <= 19)

Quiz

This quiz has 5 questions. Pick an answer to see why it is right or wrong.

  1. 1What does this program print?

    print(True + True + False)
  2. 2What does this program print?

    print(bool("False"))
  3. 3Which operator checks that two values are NOT equal?

  4. 4What does this program print?

    x = 10
    print(5 < x < 15)
  5. 5Which None check does PEP 8 recommend?

Frequently asked questions

Is True == 1 in Python?
Yes. bool is a subtype of int, so True == 1 and False == 0 are both True, and True + True is 2.
Should I write if x is True or if x == True?
Neither, usually. The usual style is a plain if x with no comparison. x == True lets 1 and 1.0 through but rejects other truthy values such as 2, and x is True accepts nothing but True itself, so use is True only when that is exactly what you need.
How does bool() work in Python?
bool() returns False for False, None, zero of any number type, and empty strings and collections, and True for almost everything else. The same rule decides what an if statement treats as True.
What are the 6 comparison operators in Python?
Equal ==, not equal !=, greater than >, less than <, greater than or equal >=, and less than or equal <=. Each returns True or False when the two values can be compared, and the Python language reference also counts is, is not, in and not in as comparison operators.
Does Python have a === operator?
No, Python has no === operator. Its == never turns a string into a number, which is the main reason people reach for === in JavaScript, but different number types can still be equal, so 1 == 1.0 is True. The is operator checks whether two names refer to the same object, which beginners need mainly for None.
How do I compare strings in Python?
With the comparison operators you use for numbers. == checks equality, and < or > compare one character at a time by each character's number in Unicode, which gives alphabetical order for unaccented letters of the same case, with uppercase letters first.