Python offers three logical operators: and, or, and not. These operators, also known as Boolean operators, evaluate multiple conditions and determine an expression's overall truth value.
In programming, we frequently need to make decisions based on multiple conditions. For instance, we might want to execute a certain action only if several conditions are met simultaneously. Alternatively, we might want to trigger an alternative action if any one of several conditions is fulfilled.
Python offers a set of logical operators – and, or, not – to handle these scenarios.
In this tutorial, we'll learn how Python logical operators work and how to use them in programming and data science. Note that we’ll use the uppercase words AND, OR, and NOT to represent these operators conceptually. And we’ll refer to the Python keywords using this convention: and, or, and not.
If you’re looking for a curated Python curriculum, check out this six-course Python Programming skill track.
Before getting into the details, let’s start with a very brief answer for those of you in a hurry.
Short Answer: What Are Python Logical Operators?
Python logical operators (and, or, not) are special keywords that act as conjunctions in our code, connecting and evaluating multiple conditions within a single expression.
Consider this example:
temperature =25
is_sunny = True
if temperature >20 and is_sunny:print("It's a perfect day for a picnic!")
Here, the message is printed only if it is not sunny.
Logical operators give us a concise and powerful way to express complex conditions, allowing our programs to make decisions based on multiple factors. Now, let’s take a deeper look and start with the and logical operator.
Python: The and Operator
In Boolean logic, the AND operation involves comparing two values or expressions. It produces a true result only if both values are true. If either one or both of the values are false, the result of the AND operation is false. Here’s a summary of how AND behaves:
Operation
Output value
true AND true
true
true AND false
false
false AND true
false
false AND false
false
We can recreate this truth table with Python's and operator:
options = True, False
for a inoptions:for b inoptions:print(f"{a} and {b} gives {a and b}")ขับเคลื่อนโดย
True and True gives True
True and False gives False
False and True gives False
False and False gives False
ขับเคลื่อนโดย
The options variable contains the two Boolean values. The nested for loops iterate through options twice to create all permutations of True and False.
Often, the expressions used as operands for and include comparison or equality operators:
names ="James","Bob","David","Jane","Kate","Mary"for name innames:iflen(name)<5 and name[0]=="J":print(name)ขับเคลื่อนโดย
The name is printed out only if it contains fewer than five letters and starts with "J". Both requirements must be met.
The comparison and equality operators, such as < and ==, evaluate to True or False.
However, the Python and operator works on any data type and not just the Boolean True and False. The output value can also be any data type.
Let's explore Python's and operator in more detail in this section. First, we'll discuss truthiness.
Python: truthy and falsy
Every Python object can be converted to a Boolean type using the built-in bool(). An object is truthy if bool(object) returns True. For a falsy object, bool(object) returns False.
Python's and operator looks at the truthiness of its operands. If both operands are truthy, the and operator also returns a truthy value. If one or both operands are falsy, and returns a falsy value. Let's look at some examples:
if42and"python":print("Both operands are truthy")else:print("One or both operands are falsy")ขับเคลื่อนโดย
Numeric types, like integers and floats, are truthy if they're non-zero and falsy when equal to zero. Sequences, like strings, are truthy when they're non-empty and falsy when they're empty.
Therefore, only the first of the three examples above satisfies the condition that both operands should be truthy for the and operator to evaluate to truthy.
The and operator doesn't always return a Boolean value. An expression that includes and could also evaluate to one of the operands. Let's dive into this further using the same examples we used earlier. Instead of using the and expressions in an if statement, we'll evaluate them directly:
print(42 and "python")print(42 and "")print(0 and "python")ขับเคลื่อนโดย
Can you spot what's happening with these expressions?
Let's start with the last of the three examples, which has 0 and "python" as the two operands. The and operator is looking for two truthy operands. Since the first one is 0, it's falsy. There's no need for the program to check the second operator since the whole expression will be falsy regardless of the value of the second operand.
However, the and operator doesn't return False. Instead, it returns the first operand directly. Since the first operand is falsy, the whole and expression evaluates to falsy. This doesn't cause any issues in Python, as in situations where Python requires a true or false value, it can use the truthiness of an object.
In the first example, the first operand is 42, which is truthy. The program evaluates the second operand and returns it. Since the second operand is the string "python", which is truthy, the whole expression evaluates as truthy.
In the second example, the first operand is also 42. Since it's truthy, the program evaluates and returns the second operand, which is the empty string. The empty string is falsy, and therefore, the entire and expression evaluates to falsy.
When the first operand is truthy, the and operator always returns the value of the second operand, whether the second operand is truthy or falsy.
When the first operand is falsy, Python doesn't evaluate the second operand. This is called short-circuiting, and it's a form of lazy evaluation. You can confirm this with the following code:
The second operand is 5/0, which would normally raise a ZeroDivisionError. However, the 5/0 expression isn't evaluated in this example since the first operand is falsy. The exception is never raised.
Example: The Programmers' Online Store
Let's consider a simple example that represents a real-life application. Transactions in an online store that supplies goods for programmers are recorded and accessed through an API. The data are converted into a list of dictionaries. Each item in the list represents a transaction and each transaction is a dictionary containing relevant information:
data =[{"Item":"Python Book","Price":50,"Quantity":1,"Success": True},{"Item":"Coffee Machine","Price":125,"Quantity":1,"Success": False},{"Item":"Headphones","Price":250,"Quantity":1,"Success": True},{"Item":"Cables","Price":15.99,"Quantity":3,"Success": True},]ขับเคลื่อนโดย
Each transaction includes the item's name and price, the quantity included in the order, and whether the transaction was successful.
Let's assume we need successful transactions for items priced at $100 or over. We can use the and operator to filter the rows we need:
for item indata:if item["Success"] and item["Price"]>=100:print(item)ขับเคลื่อนโดย
The output from this code shows the only transaction that meets both requirements. For each item in the list of transactions, the if statement checks the truthiness of two expressions:
The values associated with the key "Success" in each dictionary are either True or False.
The greater than or equal to operator in item["Price"] >= 100 also returns True or False.
The Python book and the cables do not meet the price requirement as they're under $100. The coffee machine is over this price threshold, but this transaction wasn't successful, so it's excluded from the printout.
We'll return to this online store example later in this tutorial.
In the following section, we'll focus on the or operator.
Python: The or Operator
The OR logical operator in Boolean logic needs two operands. It evaluates to true if at least one of the operands is true:
Operation
Output value
true OR true
true
true OR false
true
false OR true
true
false OR false
false
We can recreate this truth table using Python's or operator:
options = True, False
for a inoptions:for b inoptions:print(f"{a} or {b} gives {a or b}")ขับเคลื่อนโดย
True or True gives True
True or False gives True
False or True gives True
False or False gives False
ขับเคลื่อนโดย
We can also revisit the example with people's names we explored earlier but replace the and operator with an or:
names ="James","Bob","David","Jane","Kate","Mary"for name innames:iflen(name)<5 or name[0]=="J":print(name)ขับเคลื่อนโดย
The if statement now contains an or expression. Therefore, the for loop prints all names that either have fewer than five letters or start with the letter "J". The string "Jane" meets both requirements. The other printed names meet one of the two requirements.
Python or operator: truthy and falsy
The or operator also looks for truthy and falsy values and not just the Booleans True and False. Let's look at some examples:
if42 or [10,20,30]:print("At least one operand is truthy")else:print("None of the operands is truthy")ขับเคลื่อนโดย
In the first example, the integer 42 and the non-empty list [10, 20, 30] are truthy. Since the or operator needs at least one operand to be truthy, the whole expression evaluates as truthy.
In the second example, 42 is truthy but the empty list [] is falsy since it's an empty sequence. As one of the two operands is truthy, the whole or expression evaluates as truthy.
In the final example, both 0 and [] are falsy. Therefore, the entire expression evaluates as falsy.
The or operator also uses short-circuiting when evaluating the expression to avoid unnecessary evaluations. Let's look at some examples to illustrate this point:
print(42 or [10,20,30])print(42 or [])print([] or 42)print([] or 0)ขับเคลื่อนโดย
In the first example, the first operand is 42, which is truthy. Since or only requires one operand to be truthy to determine the final truthiness of the entire expression, this first operand is returned immediately. The second operand is not evaluated as its value has no effect on the final result. The or operator returns the first operand, 42, not the Boolean True. In Python, any truthy value has the same effect as the Boolean True in situations such as if statements and while statements.
In the second example, the first operand is also 42, and the expression returns this integer even though the second operand is falsy.
In the third and fourth examples, the first operand is the empty list, which is falsy. Therefore, the second operand is returned in both cases.
Example: The Programmers' Online Store
Let's revisit the transactions from the online store that sells goods to programmers. Here are the four sample transactions we're using in this example:
data =[{"Item":"Python Book","Price":50,"Quantity":1,"Success": True},{"Item":"Coffee Machine","Price":125,"Quantity":1,"Success": False},{"Item":"Headphones","Price":250,"Quantity":1,"Success": True},{"Item":"Cables","Price":15.99,"Quantity":3,"Success": True},]ขับเคลื่อนโดย
You need to identify some transactions to investigate customer behavior further. You want to find the transactions that either failed or include an item priced below $75. Since you have two conditions and you only require a transaction to meet at least one of them, you can use the or operator:
for item indata:if item["Success"]== False or item["Price"]<75:print(item)ขับเคลื่อนโดย
This for loop displays the lines that match at least one of the requirements. The Python book and the cables are included as their price is below $75. The coffee machine is included since the transaction wasn't successful.
There's an alternative way of filtering the unsuccessful transactions, which we'll explore in the following section.
Python: The not Operator
The NOT operator differs from AND and OR since it only requires one operand. NOT converts true to false and the other way round. Here's the truth table for NOT:
Operation
Output value
NOT true
false
NOT false
true
We can replicate this using Python's not operator:
options = True, False
for a inoptions:print(f"not {a} evaluates to {not a}")ขับเคลื่อนโดย
not True evaluates to False
not False evaluates to True
ขับเคลื่อนโดย
Python not operator: truthy and falsy
As with and and or, the operand used with not doesn't have to be a Boolean. Since any object is either truthy or falsy, any object can be used with not.
if not "python":print("The `if` statement evaluated as `True`")else:print("The `if` statement evaluated as `False`")ขับเคลื่อนโดย
In the previous sections, we used the transactions from an online store. We want to display all the unsuccessful transactions in this example. One option is using the equality operator == to check whether data["Success"] equals False. However, we can also use the not operator instead:
data =[{"Item":"Python Book","Price":50,"Quantity":1,"Success": True},{"Item":"Coffee Machine","Price":125,"Quantity":1,"Success": False},{"Item":"Headphones","Price":250,"Quantity":1,"Success": True},{"Item":"Cables","Price":15.99,"Quantity":3,"Success": True},]for item indata:if not item["Success"]:print(item)ขับเคลื่อนโดย
Since item["Success"] is equal to False for the unsuccessful transactions, not item["Success"] returns True. Therefore, the for loop prints out the unsuccessful transactions:
In the following section, we'll learn how to combine several logical operators in one expression.
Python: Operator Precendence
In the examples shown so far, we only used one of the logical operators in each instance. In this section, we'll see what happens when we combine more logical operators in a single expression.
Python: and has precedence over or
We'll continue working on the transactions from the online store we used earlier. Let's try the following conditional statement:
data =[{"Item":"Python Book","Price":50,"Quantity":1,"Success": True},{"Item":"Coffee Machine","Price":125,"Quantity":1,"Success": False},{"Item":"Headphones","Price":250,"Quantity":1,"Success": True},{"Item":"Cables","Price":15.99,"Quantity":3,"Success": True},]
for item indata:if item["Price"]>100 or item["Quantity"]>2 and item["Success"]:print(item)ขับเคลื่อนโดย
The if statement contains two logical operators. Before we investigate how Python deals with this expression, let's look at an English translation of the if statement: If the item's price is over $100 or the quantity sold is greater than two and the sale was successful, then print the item.
This phrase is ambiguous without more punctuation marks and may still cause confusion even if we add more commas in the sentence. For example, should the transaction with the coffee machine be included? The item's price is over the required threshold but the transaction wasn't successful.
Python resolved this ambiguity by giving the and operator precedence over the or operator. This means that the and expression is evaluated first, and its outcome is used in the or expression.
Therefore, the expression item["Quantity"] > 2 and item["Success"] is evaluated first. This expression returns a truthy value only for the transaction including cables. It evaluates as falsy for the remaining items.
The value from the and expression is then used with the or operator. The or expression returns a truthy value for any item that returns truthy for the and expression—this was only the transaction including cables—and for items with a price over $100. There are two items with prices over the threshold: the coffee machine and the headphones.
Python: parentheses and logical operators
However, what if we need transactions with items priced above $100 or with a quantity above two, but we also want to ensure the transaction is successful? In this case, we want the or expression to take precedence over the and so that it's evaluated first. We can achieve this using parentheses around the or expression:
for item indata:if(item["Price"]>100 or item["Quantity"]>2) and item["Success"]:print(item)ขับเคลื่อนโดย
The additional parentheses give precedence to the or expression. The coffee machine transaction is no longer included since it evaluates falsy for the second and operand.
Python: not has precedence over and and or
The not operator takes precedence over and and or. Therefore, not is evaluated before the other two logical operators.
Let's consider the following if statement, which includes both not and and:
for item indata:if not item["Success"] and item["Price"]>100:print(item)ขับเคลื่อนโดย
Since not is evaluated first, this expression will first apply the not operator to item["Success"]. The value from the not expression is then used as the first operand in the and expression. Only the coffee machine entry satisfies this condition—this is the only transaction that has failed and has a price over $100.
However, we can give precedence to the and operator by using parentheses:
for item indata:ifnot(item["Success"] and item["Price"]>100):print(item)ขับเคลื่อนโดย
In this case, the and expression is evaluated first. Only the entry with headphones evaluates to True since it's the only successful transaction with a price over $100. However, we add a not operator before the expression in parentheses. Therefore, the code outputs the remaining three transactions.
In general, if an expression becomes too complex because it has too many operators, it's best to split it to make it more readable.
Conclusion
The Python logical operators and, or, and not are used extensively in a broad range of programming and data science applications. These operators replicate the behavior of the AND, OR, and NOT Boolean logical operators used broadly in logic and mathematics.
However, the Python operators can accept any object and not just Boolean values. The and and or operators use short-circuit evaluation and return one of the operands as their output.
The Python logical operators are key tools to filter data, identify features in data sets, and create complex rules to control the flow of a program. If you want to learn more about this, check out this nine-course Data Analyst With Python career track.
I studied Physics and Mathematics at UG level at the University of Malta. Then, I moved to London and got my PhD in Physics from Imperial College. I worked on novel optical techniques to image the human retina. Now, I focus on writing about Python, communicating about Python, and teaching Python.
Python Logical Operators FAQs
Can I use the and operator to test multiple conditions at once?⌄
Yes, you can chain multiple conditions together using the and operator. For example: if x > 5 and x < 10 and x != 7.
What's the difference between the and operator and the bitwise AND operator (&)?⌄
The and operator performs a logical AND operation on Boolean values, returning True only if both operands are True. The bitwise AND operator (&) performs a bitwise AND operation on the binary representation of integer values.
Does Python have the && and || operators like some other languages?⌄
No, Python uses the keywords and and or instead of the symbols && and || for logical operations.
Comparing values in Python to check if they are not equal is simple with the not equal operator. Check out this quick tutorial on how to use the not equal Python operator, as well as alternatives for comparing floats.
Learn about relational operators for comparing R objects and logical operators for combining boolean TRUE and FALSE values. You'll also construct conditional statements.
In programming, we frequently need to make decisions based on multiple conditions. For instance, we might want to execute a certain action only if several conditions are met simultaneously. Alternatively, we might want to trigger an alternative action if any one of several conditions is fulfilled.
Python offers a set of logical operators –
and,or,not– to handle these scenarios.In this tutorial, we'll learn how Python logical operators work and how to use them in programming and data science. Note that we’ll use the uppercase words AND, OR, and NOT to represent these operators conceptually. And we’ll refer to the Python keywords using this convention:
and,or, andnot.If you’re looking for a curated Python curriculum, check out this six-course Python Programming skill track.
Before getting into the details, let’s start with a very brief answer for those of you in a hurry.
Short Answer: What Are Python Logical Operators?
Python logical operators (
and,or,not) are special keywords that act as conjunctions in our code, connecting and evaluating multiple conditions within a single expression.Consider this example:
Here, the and operator ensures that the message is printed only if
bothconditions are met: the temperature is above 20 degreesandit's sunny.Let's look at another example using the
oroperator:In this case, we'll head out for a walk if either it's sunny or we have an umbrella.
Finally, the
notoperator flips the truth value of a condition:Here, the message is printed only if it is not sunny.
Logical operators give us a concise and powerful way to express complex conditions, allowing our programs to make decisions based on multiple factors. Now, let’s take a deeper look and start with the
andlogical operator.Python: The
andOperatorIn Boolean logic, the AND operation involves comparing two values or expressions. It produces a true result only if both values are true. If either one or both of the values are false, the result of the AND operation is false. Here’s a summary of how AND behaves:
Operation
Output value
true AND true
true
true AND false
false
false AND true
false
false AND false
false
We can recreate this truth table with Python's
andoperator:The
optionsvariable contains the two Boolean values. The nestedforloops iterate through options twice to create all permutations ofTrueandFalse.Often, the expressions used as operands for and include comparison or equality operators:
The name is printed out only if it contains fewer than five letters and starts with
"J". Both requirements must be met.The comparison and equality operators, such as
<and==, evaluate toTrueorFalse.However, the Python
andoperator works on any data type and not just the BooleanTrueandFalse. The output value can also be any data type.Let's explore Python's
andoperator in more detail in this section. First, we'll discuss truthiness.Python: truthy and falsy
Every Python object can be converted to a Boolean type using the built-in
bool(). An object is truthy ifbool(object)returnsTrue. For a falsy object,bool(object)returnsFalse.Python's
andoperator looks at the truthiness of its operands. If both operands are truthy, theandoperator also returns a truthy value. If one or both operands are falsy,andreturns a falsy value. Let's look at some examples:Now, let's consider this example:
And let's take a look at this final example:
Numeric types, like integers and floats, are truthy if they're non-zero and falsy when equal to zero. Sequences, like strings, are truthy when they're non-empty and falsy when they're empty.
Therefore, only the first of the three examples above satisfies the condition that both operands should be truthy for the
andoperator to evaluate to truthy.The
andoperator doesn't always return a Boolean value. An expression that includesandcould also evaluate to one of the operands. Let's dive into this further using the same examples we used earlier. Instead of using theandexpressions in anifstatement, we'll evaluate them directly:Can you spot what's happening with these expressions?
Let's start with the last of the three examples, which has
0and"python"as the two operands. Theandoperator is looking for two truthy operands. Since the first one is0, it's falsy. There's no need for the program to check the second operator since the whole expression will be falsy regardless of the value of the second operand.However, the
andoperator doesn't returnFalse. Instead, it returns the first operand directly. Since the first operand is falsy, the wholeandexpression evaluates to falsy. This doesn't cause any issues in Python, as in situations where Python requires a true or false value, it can use the truthiness of an object.In the first example, the first operand is
42, which is truthy. The program evaluates the second operand and returns it. Since the second operand is the string"python", which is truthy, the whole expression evaluates as truthy.In the second example, the first operand is also
42. Since it's truthy, the program evaluates and returns the second operand, which is the empty string. The empty string is falsy, and therefore, the entireandexpression evaluates to falsy.When the first operand is truthy, the
andoperator always returns the value of the second operand, whether the second operand is truthy or falsy.When the first operand is falsy, Python doesn't evaluate the second operand. This is called short-circuiting, and it's a form of lazy evaluation. You can confirm this with the following code:
The second operand is
5/0, which would normally raise aZeroDivisionError. However, the5/0expression isn't evaluated in this example since the first operand is falsy. The exception is never raised.Example: The Programmers' Online Store
Let's consider a simple example that represents a real-life application. Transactions in an online store that supplies goods for programmers are recorded and accessed through an API. The data are converted into a list of dictionaries. Each item in the list represents a transaction and each transaction is a dictionary containing relevant information:
Each transaction includes the item's name and price, the quantity included in the order, and whether the transaction was successful.
Let's assume we need successful transactions for items priced at $100 or over. We can use the
andoperator to filter the rows we need:The output from this code shows the only transaction that meets both requirements. For each item in the list of transactions, the
ifstatement checks the truthiness of two expressions:"Success"in each dictionary are eitherTrueorFalse.item["Price"] >= 100also returnsTrueorFalse.The Python book and the cables do not meet the price requirement as they're under $100. The coffee machine is over this price threshold, but this transaction wasn't successful, so it's excluded from the printout.
We'll return to this online store example later in this tutorial.
In the following section, we'll focus on the
oroperator.Python: The
orOperatorThe OR logical operator in Boolean logic needs two operands. It evaluates to true if at least one of the operands is true:
Operation
Output value
true OR true
true
true OR false
true
false OR true
true
false OR false
false
We can recreate this truth table using Python's
oroperator:We can also revisit the example with people's names we explored earlier but replace the
andoperator with anor:The
ifstatement now contains anorexpression. Therefore, theforloop prints all names that either have fewer than five letters or start with the letter"J". The string"Jane"meets both requirements. The other printed names meet one of the two requirements.Python
oroperator: truthy and falsyThe or operator also looks for truthy and falsy values and not just the Booleans
TrueandFalse. Let's look at some examples:Now let’s consider this example:
And let’s also consider this final example:
In the first example, the integer
42and the non-empty list[10, 20, 30]are truthy. Since theoroperator needs at least one operand to be truthy, the whole expression evaluates as truthy.In the second example,
42is truthy but the empty list[]is falsy since it's an empty sequence. As one of the two operands is truthy, the whole or expression evaluates as truthy.In the final example, both
0and[]are falsy. Therefore, the entire expression evaluates as falsy.The
oroperator also uses short-circuiting when evaluating the expression to avoid unnecessary evaluations. Let's look at some examples to illustrate this point:In the first example, the first operand is
42, which is truthy. Sinceoronly requires one operand to be truthy to determine the final truthiness of the entire expression, this first operand is returned immediately. The second operand is not evaluated as its value has no effect on the final result. The or operator returns the first operand,42, not the BooleanTrue. In Python, any truthy value has the same effect as the BooleanTruein situations such asifstatements and while statements.In the second example, the first operand is also
42, and the expression returns this integer even though the second operand is falsy.In the third and fourth examples, the first operand is the empty list, which is falsy. Therefore, the second operand is returned in both cases.
Example: The Programmers' Online Store
Let's revisit the transactions from the online store that sells goods to programmers. Here are the four sample transactions we're using in this example:
You need to identify some transactions to investigate customer behavior further. You want to find the transactions that either failed or include an item priced below $75. Since you have two conditions and you only require a transaction to meet at least one of them, you can use the
oroperator:This
forloop displays the lines that match at least one of the requirements. The Python book and the cables are included as their price is below $75. The coffee machine is included since the transaction wasn't successful.There's an alternative way of filtering the unsuccessful transactions, which we'll explore in the following section.
Python: The
notOperatorThe NOT operator differs from AND and OR since it only requires one operand. NOT converts true to false and the other way round. Here's the truth table for NOT:
Operation
Output value
NOT true
false
NOT false
true
We can replicate this using Python's
notoperator:Python
notoperator: truthy and falsyAs with
andandor, the operand used withnotdoesn't have to be a Boolean. Since any object is either truthy or falsy, any object can be used withnot.Let’s also look at this example:
Truthy values are converted to
Falsewith thenotoperator and falsy values are converted toTrue.Unlike
andandor, thenotoperator always returns a Boolean object:Example: The Programmers' Online Store
In the previous sections, we used the transactions from an online store. We want to display all the unsuccessful transactions in this example. One option is using the equality operator
==to check whetherdata["Success"]equalsFalse. However, we can also use thenotoperator instead:Since
item["Success"]is equal toFalsefor the unsuccessful transactions, notitem["Success"]returnsTrue. Therefore, the for loop prints out the unsuccessful transactions:In the following section, we'll learn how to combine several logical operators in one expression.
Python: Operator Precendence
In the examples shown so far, we only used one of the logical operators in each instance. In this section, we'll see what happens when we combine more logical operators in a single expression.
Python:
andhas precedence overorWe'll continue working on the transactions from the online store we used earlier. Let's try the following conditional statement:
The
ifstatement contains two logical operators. Before we investigate how Python deals with this expression, let's look at an English translation of theifstatement: If the item's price is over $100 or the quantity sold is greater than two and the sale was successful, then print the item.This phrase is ambiguous without more punctuation marks and may still cause confusion even if we add more commas in the sentence. For example, should the transaction with the coffee machine be included? The item's price is over the required threshold but the transaction wasn't successful.
Python resolved this ambiguity by giving the
andoperator precedence over theoroperator. This means that theandexpression is evaluated first, and its outcome is used in theorexpression.Therefore, the expression
item["Quantity"] > 2 and item["Success"]is evaluated first. This expression returns a truthy value only for the transaction including cables. It evaluates as falsy for the remaining items.The value from the and expression is then used with the
oroperator. Theorexpression returns a truthy value for any item that returns truthy for theandexpression—this was only the transaction including cables—and for items with a price over $100. There are two items with prices over the threshold: the coffee machine and the headphones.Python: parentheses and logical operators
However, what if we need transactions with items priced above $100 or with a quantity above two, but we also want to ensure the transaction is successful? In this case, we want the
orexpression to take precedence over theandso that it's evaluated first. We can achieve this using parentheses around theorexpression:The additional parentheses give precedence to the
orexpression. The coffee machine transaction is no longer included since it evaluates falsy for the secondandoperand.Python:
nothas precedence overandandorThe
notoperator takes precedence overandandor. Therefore,notis evaluated before the other two logical operators.Let's consider the following if statement, which includes both
notandand:Since
notis evaluated first, this expression will first apply the not operator toitem["Success"]. The value from thenotexpression is then used as the first operand in the and expression. Only the coffee machine entry satisfies this condition—this is the only transaction that has failed and has a price over $100.However, we can give precedence to the
andoperator by using parentheses:In this case, the
andexpression is evaluated first. Only the entry with headphones evaluates toTruesince it's the only successful transaction with a price over $100. However, we add anotoperator before the expression in parentheses. Therefore, the code outputs the remaining three transactions.In general, if an expression becomes too complex because it has too many operators, it's best to split it to make it more readable.
Conclusion
The Python logical operators
and,or, andnotare used extensively in a broad range of programming and data science applications. These operators replicate the behavior of the AND, OR, and NOT Boolean logical operators used broadly in logic and mathematics.However, the Python operators can accept any object and not just Boolean values. The
andandoroperators use short-circuit evaluation and return one of the operands as their output.The Python logical operators are key tools to filter data, identify features in data sets, and create complex rules to control the flow of a program. If you want to learn more about this, check out this nine-course Data Analyst With Python career track.