Many of your logical expressions will simply contain a few, clear phrases. For example, the expression "A < B or C >= D" clearly says to "or" together the results of the conditions "A < B" and "C >= D". However, you may need to chain together multiple operators to test for increasingly complicated conditions. When that happens, if Python doesn't execute the individual operators in the order you desire, the results can be unexpected.

Uncertain Evaluation Order
Imagine that you want to run a block of code when both of the following conditions are True:

playerScore > 100
age < 13 or age > 18
So, the overall expression should be True if the playerScore is greater than 100 and the age is less than 13 or greater than 18. You might begin to write a Python "if" statement as follows:

if (playerScore > 100 and age < 13 or age > 18):
Copy
How will Python interpret this code? Will it test "playerScore > 100" first, then "and" together the results of "age > 13" or "age <= 18" next, and finally "or" together the results as you intended? That evaluation path is shown on the left side of the image below.

Alternate evaluation paths

Or, will the code actually test "playerScore > 100" first, then "and" that result to "age < 13", and finally "or" that result to "age > 18"? That evaluation order can produce an entirely different result, as shown on the right side of the image above. Python does not know what you intended, only what you have written in the code.

Without any more guidance from you, the programmer, Python will follow its own internal rules (called operator precedence) for evaluating expressions in a default order. There's a great chance that default order doesn't meet your program's needs, so you will want to use parentheses to make it clear to Python - and anyone reading your code - exactly how the expression will be evaluated.

Using Parentheses with Logical Operators
Any time you surround an operation in parentheses ( ), that tells Python to evaluate the operation first. Then, take the result and use it in any surrounding expression outside the parentheses. Let's re-write our example with parentheses to make sure it works correctly.

if ((playerScore > 100) and (age < 13 or age > 18)):
Copy
With these parentheses, we have told Python to evaluate "playerScore > 100" and "age < 13 or age > 18" first, independently. Those True or False results are then combined with "and" to produce the final answer.

Using Parentheses to specify evaluation paths

It is best practice to always use parentheses to make your logic perfectly clear! That way, both Python and anyone else that reads your code will understand your intent and evaluate the expression correctly.

Experiment with the sample code below. Try setting different values for the playerScore and age variables, then see the results from logical expressions that are written with and without the guiding parentheses.

Try It Now


Try changing the initial values for playerScore and age to make each of the individual comparisons True or False. Can you find a combination where the two results are not the same? If the playerScore is below 100 and the age is above 18, for example, these two expressions give different answers. Only the second expression, when parentheses are used, will always produce results to match our original logic requirements.

Review - Mathematical, Comparison and Logical Operators
You have now seen quite a few Python symbols that let you perform mathematical operations, compare values, or join Boolean expressions. It's easy to get them confused, so let's quickly review each group of operators.

Mathematical Operators:

Mathematical operators are used to add, subtract, multiply and divide numeric values to produce a numeric result.

Symbol Description Example Statement
+ Addition numericResult = 1 + 2
- Subtraction numericResult = 5 - 3
* Multiplication numericResult = 4 * 2
/ Division numericResult = 10 / 5
Comparison Operators:

Comparison operators are used to compare two values of any type and produce a Boolean (True/False) result.

Symbol Description Example Statement
== Equal To booleanResult = 1 == 2
!= Not Equal To booleanResult = 1 == 2
< Less Than booleanResult = 1 < 2
<= Less Than or Equal To booleanResult = 1 <= 2
> Greater Than booleanResult = 1 > 2
>= Greater Than or Equal To booleanResult = 1 >= 2
Logical Operators:

Logical operators are used to join two Boolean True/False values or flip a single Boolean value.

Symbol Description Example Statement
and True if A AND B are True booleanResult = (1 == 2) and (3 < 4)
or True if A OR B are True booleanResult = (1 == 2) or (3 < 4)
not True if A is False or False if A is True booleanResult = not (1 > 2)
Mixing Operators and Parentheses
You can write very lengthy logical expressions with careful use of parentheses. Just make sure that you are applying mathematical operators to numeric values, comparison operators to any type of data, and logical operators to Boolean variables!

You can also use parentheses to help clarify mathematical expressions. What will happen when you write the following expression?

result = 1 + 3 * 5
Copy
Will "1 + 3" happen first giving you 4, then that result is multiplied by 5 to reach 20? Or, will 3 * 5 happen first giving you 15, then 1 is added to that result to reach 16?

Just as with logical expressions, Python has a default evaluation order for mathematical operators, and it is not necessarily left-to-right. So, instead of trying to understand and apply the internal Python rules, it is best practice to always use parentheses with your mathematical operations to make sure they behave exactly the way you want. The example below uses parentheses to force the "1 + 3" operation to happen first, and therefore the resulting value is 20.

result = (1 + 3) * 5
Copy
Now, let's explore a few more examples that use a mixture of mathematical, comparison and logical operators. Each of the expressions below is valid and will produce a Boolean result. Can you figure out what the result will be in each case, before you run the example code?

Try It Now


You are encouraged to experiment with the initial values and the expressions themselves to form new results. Can you correctly predict the result each time you make a change?

When using parentheses, every opening parenthesis "(" must be matched by exactly one closing parenthesis ")" somewhere in that same statement. Carefully ensure you have the same number of opening and closing parentheses in every statement, and that each matched pair clearly surrounds the expression or expressions that you want to mark.
Reminder - Breaking Long Statements across Multiple Lines
When writing long logical expressions, you might not be able to fit the entire expression on one code line comfortably. Remember that Python allows you to break a long statement across multiple lines. Simply add a backslash (\) to mark a split and continuation of that statement on the next line.

The following two code examples each contain a single statement that does the same thing.

result = (A < 3) or (B > 5) and ((C + D) == 42)
Copy
result = (A < 3) or \
(B > 5) and \
((C + D) == 42)
Copy
You may find one version easier to read than the other. You can decide to break long lines at certain spots (or not) in ways that make sense to you.....

In summary, when using logical operators in Python, it is important to consider the order of evaluation and use parentheses to specify the desired evaluation path. This helps ensure that the expression is evaluated as intended and avoids unexpected results. It is also important to understand the different groups of operators: mathematical operators, comparison operators, and logical operators. Mixing operators and using parentheses can help clarify complex expressions. Additionally, when writing long logical expressions, it is possible to break the statement across multiple lines using the backslash (\) character.

1. What is the best way to make certain all parts of a complex logical expression evaluate in the order that your program requires?

Select one:

a.
Write expressions from left to right and hope for the best

b.
Use parentheses to mark expressions that should evaluate first

c.
Always use nested "if" statements to break down complex expressions

d.
Break statements across multiple lines, with highest priority expressions coming first

2. Given the code below, which expression placed in the ???? location will ensure a False result?
A = False
B = True
C = True
if (????):
Select one:

a.
A and B or C

b.
A and (B or C)

c.
(A and B) or C

d.
A or B or C

3. Which of the following is NOT a comparison operator?
Select one:

a.
and

b.
+

c.
None of these are comparison operators

d.
or

4. What type of value is held in the "answer" variable after the following code runs?
answer = (1 + 5) >= (6 - 1)
Select one:

a.
Boolean

b.
Floating point

c.
Integer

d.
String

5. Given the following code, what can be said about result1 and result2 ?
result1 = (A < 3) or (B > 5) and ((C + D) == 42)
result2 = (A < 3) or \
(B > 5) and \
((C + D) == 42)
Select one:

a.
result1 and result2 will contain different values

b.
result1 will be True and result2 will be False

c.
None of these is valid

d.
result1 and result2 will contain the same value

1. b. Use parentheses to mark expressions that should evaluate first.

2. b. A and (B or C).
3. a. and.
4. a. Boolean.
5. d. result1 and result2 will contain the same value.