Let's talk about conditional operators in Python.
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
True
The == operator will respond with either True or False.
>>> a == c
False
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.
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 greet.py 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 greet.py
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
True
Booleans usually appear as the result of a comparison operation.
The equals-equals operator (==) is our equality operator in Python.
>>> n = -4
>>> n == 0
False
Python also has an inequality operator, != (sometimes called the not equals operator):
>>> n != 0
True
There's also less than and greater than:
>>> n < 0
True
>>> n > 0
False
And less than or equal to and greater than or equal to:
>>> n <= 0
True
>>> n >= 0
False
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
False
And there's a not in operator for doing the opposite (checking for a lack of containment):
>>> n not in numbers
True
There's also the is operator and the is not operator for checking identity:
>>> n = 5
>>> n is None
False
>>> n is not None
True
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).
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.
Python Jumpstart is designed to help new Python programmers get up to speed quickly. Get hands-on practice with 50 bite-sized modules that build your Python skills step by step.
Sign up for my 5 day email course and learn essential concepts that introductory courses often overlook!
Sign in to your Python Morsels account to track your progress.
Don't have an account yet? Sign up here.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces. Learn to avoid beginner pitfalls, in less than a week!
Ready to level up? Sign up now to begin your Python journey the right way!