One of the major reasons that programs are useful is because they can do different things depending on their input. To be able to make these decisions, we need to be able to write “logical expressions”, sometimes also known as “conditional expressions”. Think of these as a question that the computer can ask about the data. We will learn about two groups of operators: comparison operators and Boolean operators. Comparison operators ask questions about two numbers’ relationship with each other. Then Boolean operators can ask questions that combine those comparisons and other questions.
==!=<><=>=Often, we want to know about the relationship between two numbers.
For this, we use the comparison operators.
Each of these operators takes two numbers and returns either True or False.
print(5 == 5) print(10 == 7) print(5.0 == 5)
The most basic test is whether two numbers are equal.
This may sound weird, but in Python we use two equal signs (==) to test for equality.
Do not use one equal sign; that means something COMPLETELY different.
print(5 != 5) print(10 != 7) print(5.0 != 5)
The second most basic test is whether two numbers are NOT equal.
The operator for this is even weirder.
In Python, to test if two things are NOT equal, we use an exclamation mark and an equal sign (!=).
print(5 < 10) print(5 <= 5) print(10 > 5) print(10 >= 10)
There are four other operators: The less than operator (<), the greater than operator (>), the less than or equal to operator (<=), and the greater than or equal to operator (>=).
These four operators ask about the order of two numbers instead of their equality.
This means that they are different from the two equality operators although they have a lot in common.
# prints True print(10 == 10) # prints False print(10 != 10)
I said before that the operators evaluate to either True or False.
Imagine the result of these operations being replaced by True or False.
If you print the value, it will literally be the text True or the text False.
and: test if two expressions are BOTH trueor: test if two expressions are EITHER or BOTH truenot: test if a single expression is falseIn the same way we can add and subtract numeric expressions together, we can also combine Boolean expressions.
There are three operators for this: and, or, and not.
# Literal boolean values print(True and True) # True print(True and False) # False print(False and True) # False print(False and False) # False # Or with math print(4 < 5 and 5 < 6) print(5 < 4 and 5 < 6) print(5 < 4 and 6 < 5)
The and operator returns True if both the left and right operands are True.
If either operand is False, then the result is also False.
Notice in the second chunk of code, we are combining the and operator with mathematical comparisons.
# Literal boolean values print(True or True) # True print(True or False) # True print(False or True) # True print(False or False) # False # Or with math print(4 < 5 or 5 < 6) print(5 < 4 or 5 < 6) print(5 < 4 or 6 < 5)
The or operator returns True if either the left or right expressions are True.
If either operand is True, then the result is also True.
This is not the opposite of and, but it is different.
# Literal boolean values print(not True) # False print(not False) # True # Or with math print(not 4 < 5) print(not 5 < 4)
The not operator returns True if the expression is False and False if the expression is True.
This is sometimes called the “negation” or “logical opposite.” Unlike the and and or operators, the not operator only takes in a single value for its operand.
You can nest logical expressions just like you can in math. Following the logic can be tricky, but just remember that the Boolean operators have the lowest priority. They will always be the last operation applied unless parentheses interrupt the order. The comparison operators have the second lowest priority. They will be applied after any math operations but before the Boolean operators.
print((5 > 7 + 3) or (not False and 3 < 2)) print(not (not (not (not True))))
Keeping this order in mind is very important when you are evaluating long, complex chains of math, comparison, and Boolean operators. The first expression shown here begins with the addition operation, then the comparisons and boolean operations.
# WRONG print(5 < 1 or 2) # Correct! print(5 < 1 or 5 < 2)
A common beginner mistake is to think that you can distribute logical expressions.
You might think that the first expression asks if the number 5 is less than 1 or less than 2.
But the or operator makes the 2 evaluated separately from the rest of the comparison.
To properly ask this question, you need to write the first statement with the “less than” operator twice.
If you run the following code as shown, then the printed value will be 2 instead of False, which you may find surprising.
In a future lesson, we will learn why this happens.
For now, just remember that you must be explicit when trying to use or and and with comparison operators.
== and !=) or their order (<, >, <=, >=).and operator evaluates to True if both operands are True.or operator evaluates to True if either or both operands are True.not operator evaluates to True if its one operand is False.