You can now write simple logical expressions like "score > 100" or "cost <= 120". But as you can imagine, a program may need to ask more complex questions such as "is a person's age both greater than 14 AND less than 21?". Python allows you to build longer logical expressions by joining together two or more shorter expressions. The resulting True or False can then be used normally in an "if" statement or stored in a Boolean variable.

Nested "if" Statements
Consider the question "Is your pet a dog named Chewy?" How might you write this question in Python code? There are actually two parts:

Is your pet a dog?
Is your dog named Chewy?
If we have stored the type of pet in a variable named pet and the pet's name in a variable called name, then we could write logical expressions as follows:

pet == "dog"
name == "Chewy"
The example below shows one possible way to run a block of code when both logical expressions are True. We use "nested if" statements, meaning one "if" statement is placed inside another "if" statement. That second "if" statement will only run if the first "if" expression is True. The code indented inside the second "if" statement will only run when both expressions are True.

Try It Now


When you run this example, you should see "This pet is a dog named Chewy" because both logical expressions are True. But if you change the pet variable to something other than "dog" or the name to something other than "Chewy", you will see a different output, because one or both logical expressions are now False.

Combining Expressions with Logical Operators
It is possible to convert a nested conditional statement into an equivalent single Boolean expression using two or more logical operators. Logical operators are keywords like "and", "or", and "not" that join or modify logical expressions in certain ways.

Each operand or input to a logical operator is a Boolean value, or some expression that evaluates to a Boolean value. In English, you could imagine the expressions listed below, where THIS and THAT are each Boolean values or expressions.

If THIS and THAT
If THIS or THAT
If not THIS
Notice that both "and" and "or" concepts have two Boolean inputs, while the "not" concept has one Boolean input.

Logical Operators: "and"
You may use the logical operator "and" to join two or more logical expressions. The resulting larger expression still evaluates to True or False. When using the and operator, if both logical expressions are True then the result is True. If either (or both) of the logical expressions are False then the overall result is False.

The example below uses "and" to join the logical expressions pet == "dog" and name == "Chewy". If both expressions are True, then the indented block of code will run, otherwise, the "else" case will run. Try it and see!

Try It Now


If you change the initial value of pet or name to anything other than "dog" and "Chewy", you will get different results, because one or both logical expressions are False.

Logical Operators: "or"
The "or" logical operator works very much the same way as "and", with one important difference. "or" will produce a True result if either one of the two logical expressions is True. You will get a False result only when both logical expressions are False.

We've changed our example to use "or" instead of "and". We also changed the initial name to "Wolife" to make the second logical expression False. What happens when you run the code below; do you get the expected results?

Try It Now


Try using different input pet and name values. You should see "or" produce True results when either one or both logical expressions are True. If you make both expressions False, you will see the message from the "else" case.

Logical Operators: "not"
Both "and" and "or" operators will join two logical expressions. The "not" operator, by contrast, will work on one logical expression. By placing "not" in front of an expression, you change the resulting Boolean value from False to True or True to False.

You could use "not" to change the result of one simple expression as shown below.

not pet == "dog"
Copy
However, you could use the "not equals" comparison operator to do exactly the same thing:

pet != "dog"
Copy
Both expressions will return True if the pet is not a "dog" and will return False if the pet is equal to a "dog".

"not" becomes more useful if you want to flip the result of a longer expression. Consider the example below.

not (pet == "dog" and name == "Chewy")
Copy
The "and" operator will first return True if both logical expressions are True. The "not" operator will then take that result and flip it. So, if the value of pet is "dog" and name is "Chewy", the results from "and" are True, but the overall result due to the "not" operator is False. If either one of the logical expressions is False, the result from "and" is False, and the overall result will be flipped to True.

Try It Now


Try changing the initial values for pet and name, and make sure you understand the resulting output from the logical expression. As you combine more operators together to form more complex expressions, the code can be harder to read and understand. So, take your time and study the results carefully!

Conditional Statements vs. Boolean Expressions
You may have noticed that you can easily write any complex conditional statements (like nested "if" statements) into an equivalent boolean expression using "and", "or", and "not". Conversely, you could rewrite any boolean expression that uses these logical operators back into an equivalent nested "if" statement. Which format is correct? This often depends on personal preference and program clarity. You should use the form that makes the most sense and is the easiest to read and understand.

1. How can you check to see if multiple logical expressions are True using nested "if" statements?
2. What is the purpose of the "and" logical operator? What results does it produce when the input expressions have any combination of True or False?
3. What is the purpose of the "or" logical operator? What results does it produce when the input expressions have any combination of True or False?
4. What is the purpose of the "not" logical operator? What result does it produce when the single input expression is True or False?

1. To check if multiple logical expressions are True using nested "if" statements, you can place one "if" statement inside another "if" statement. The nested "if" statement will only run if the condition of the outer "if" statement is True.

2. The purpose of the "and" logical operator is to join two or more logical expressions and produce a True result only if both expressions are True. If either (or both) of the input expressions are False, the overall result is False.

3. The purpose of the "or" logical operator is to join two or more logical expressions and produce a True result if at least one of the expressions is True. It only produces a False result if both input expressions are False.

4. The purpose of the "not" logical operator is to negate or flip the result of a single input expression. If the input expression is True, "not" will produce False. If the input expression is False, "not" will produce True.