Conditional operators in Python

Copied to clipboard.
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read 2 min. video Python 3.8—3.12

Let's talk about conditional operators in Python.

What are Booleans?

We have three lists here, two of which represent the same data.

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> c = [4, 5, 6]

If we wanted to ask whether two objects represent the same thing in Python, we can use the equals-equals (==) operator (a.k.a. the "equality operator"):

>>> a == b

The == operator will respond with either True or False.

>>> a == c

True and False are both of the type bool, which stands for Boolean:

>>> type(True)
<class 'bool'>

Booleans are how we represent "yes and no" or "affirmative and negative" in Python.

Booleans are often returned when using comparison operations, like equality (==).

Expressions that return either True or False (like a == b) are sometimes called Boolean expressions.

Where are Boolean expressions used?

Boolean expressions are often used in if statements to control whether a block of code should be run:

>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> if a == b:
...     print("The two lists are equal")
The two lists are equal

But they can also be used in other contexts, such as while loops.

Here's a program which uses a while loop:

name = ""
while name == "":
    # Loop until non-blank name is entered
    name = input("Please enter your name: ")

print(f"Hi {name}!")

We're looping as long as the user hasn't entered a name (as long as name is an empty string). Once they enter a name, we break out of our loop, and then we print out their name:

$ python3
Please enter your name:
Please enter your name:
Please enter your name:
Please enter your name: Trey
Hi Trey!

Boolean expressions can also be stored in a variable.

Here, we're asking whether the length of our colors list is not equal to the length of the set of colors:

>>> colors = ["purple", "green", "blue", "purple", "yellow"]
>>> has_duplicates = len(colors) != len(set(colors))

It turns out, that's True (because we have a duplicate color and sets remove duplicates):

>>> has_duplicates

Examples of comparison operations

Booleans usually appear as the result of a comparison operation.

The equals-equals operator (==) is our equality operator in Python.

>>> n = -4
>>> n == 0

Python also has an inequality operator, != (sometimes called the not equals operator):

>>> n != 0

There's also less than and greater than:

>>> n < 0
>>> n > 0

And less than or equal to and greater than or equal to:

>>> n <= 0
>>> n >= 0

Python also has a containment operator, the in operator which checks whether something is contained within a collection (e.g. contained in a list):

>>> n = 5
>>> numbers = [2, 1, 3, 4, 7]
>>> n in numbers

And there's a not in operator for doing the opposite (checking for a lack of containment):

>>> n not in numbers

There's also the is operator and the is not operator for checking identity:

>>> n = 5
>>> n is None
>>> n is not None

You usually won't want to check for identity though. You'll typically want to check for equality instead of identity (see equality vs identity in Python).

Conditional operators return Boolean values

Python's conditional operators return Booleans (i.e. True and False values) which can be used to control the flow of your code with if statements and while loops.

Series: Conditionals

Conditionals statements (if statements) are useful for making a branch in our Python code. If a particular condition is met, we run one block of code, and if not then we run another block.

To track your progress on this Python Morsels topic trail, sign in or sign up.

Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.