HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

9) Operators and Booleans Lesson

Python Operator Precedence

10 min to complete · By Martin Breuss

By now, you got to know a whole bunch of different operators that you can use in Python. And even though you learned about them in different lessons, you can mix and match and use them together in your code.

But once you start using more than one operator in an expression, you'll have to know which of them gets evaluated first! What is each operator's precedence? Just like in math, depending on what order the values in an equation are written, you can get different results.

Operator Precedence Example

What would you expect 5 + 5 * 10 to equal? Would it evaluate to 100 or 55:

5 + 5 * 10

Python follows rules similar to mathematics regarding the order of execution of different operators. Try it out in the code playground below:

a = 5 + 5 * 10
print(a)

Did you get it right? Generally, the operator precedence in Python works the same as it does in mathematics. However, since Python includes some concepts that you might not know from mathematics, it can be helpful to consult a table.

Operator Precedence Table

Check out the table below describing operator precedence in Python. It is ordered from highest precedence to lowest:

Operators Description
** Exponentiation
*, /, %, // Multiplication, division, modulo, floor division
+, - Addition and subtraction
<=, <, >, >= Comparison operators
==, != Equality operators
=, %=, /=, //=, -=, +=, *=, **= Assignment operators
is, is not Identity operators
in, not in Membership operators
not, or, and Logical operators

While this is already a long list, it still isn't a complete list of all Python operators. This course focuses on the most commonly used ones. Feel free to hop over to your favorite search engine and look for a complete list if your mind is ready to soak up some additional, more curious, and lesser-used operators. Or you can rest assured that this is all you need to know for now and keep moving.

Colorful illustration of a light bulb

Info: There's no need to memorize this table. Just keep in mind that there is a rule to operator precedence. Just like for many things in programming, being aware of a concept is the most important step. You can always come looking for this information or find it online when you need it.

Parentheses and Operator Precedence

If you ever feel unsure about what the operator precedence will be in an expression, then you can use parentheses to make sure it gets evaluated as you want it to. Yet another thing that Python shares with mathematics. Parentheses are helpful because they can make the intended precedence clear and easier to understand in an expression.

For example, the order of execution can easily be confusing when you think of a long-expression with many different operators:

print(2 ** 3 + 12 == 20 and "hello" != "hussah!")

What is the result of this expression? It's hard to say at first glance! Python has no problem evaluating this quickly, but you always want to write your code so that it's easily readable for a human. Adding parentheses can help:

print(((2 ** 3 + 12) == 20) and ("hello" != "hussah!"))

Both expressions above are exactly the same for your Python interpreter, but the second one is easier to parse for a human reader.

Colorful illustration of a light bulb

Info: There is a balance when using parentheses, so there is no need to go overboard with them. But when you see some long and complicated expression that uses a set of different operators, remember that you can use parentheses to structure and clarify what you want to achieve.

In the example above, you didn't change the order of execution and used parenthesis only for emphasis. However, just like in mathematics, you can also use them to change the order in which parts of your code should be executed:

print(2 ** 3 + 12 == (20 and "hello") != "hussah!")

Using the exact same values, you can get a different result just by placing the parentheses in different ways. This changes the order in which the different parts of this expression get executed and produces a different result.

Operator Precedence Practice

  • Try using the operators you learned in the code playground above and see if they evaluate how you expect.
  • Wrap parts of your code in parentheses and watch how it influences the order in which operators get executed.

Operators and Booleans Recap

Python follows a strict order of precedence when evaluating expressions with more than one operator. Generally, this order follows how it's done in mathematics. When you're unsure, look it up!

This lesson wraps up the section about operators and Booleans. You learned about many Python operators and their use cases:

  • Assignment Operator: Refer a symbol to a value
  • Arithmetic Operators: Do arithmetic calculations
  • Membership Operator: Checks whether an element is part of a collection
  • Relational Operators: Compare two or more values to each other
  • Logical Operators: Compare values on the basis of logic
  • Identity Operator: Finds out whether two values point to the same object in memory

You also got to know the Boolean values True and False and saw how Python uses it frequently to respond to questions you ask with many of the operators you learned.

In the upcoming section, you'll get to use operators and Boolean values to let your code take decisions in conditional statements, and learn about the loop construct that allows you to perform an action on each element of a collection.

If you've been looking for more action when coding, it's about to get exciting for you! :)

Summary: Operator Precedence

  • Operator precedence determines the order in which mathematical operations are done
  • You can highlight operations in parenthesis to make equations easier to parse for the human brain; Python reads these just the same