Skip to ContentGo to accessibility page
Introduction to Python Programming

4.4 Operator precedence

Introduction to Python Programming4.4 Operator precedence

4.4 Operator precedence

Learning objectives

By the end of this section you should be able to

  • Describe how precedence impacts order of operations.
  • Describe how associativity impacts order of operations.
  • Explain the purpose of using parentheses in expressions with multiple operators.

Precedence

When an expression has multiple operators, which operator is evaluated first? Precedence rules provide the priority level of operators. Operators with the highest precedence execute first. Ex: 1 + 2 * 3 is 7 because multiplication takes precedence over addition. However, (1 + 2) * 3 is 9 because parentheses take precedence over multiplication.

Operator Meaning
() Parentheses
** Exponentiation (right associative)
*, /, //, % Multiplication, division, floor division, modulo
+, - Addition, subtraction
<, <=, >, >=, ==, != Comparison operators
not

Logical not operator

and

Logical and operator

or

Logical or operator

Table 4.4 Operator precedence from highest to lowest.

Checkpoint

Concepts in Practice

Precedence rules

Which part of each expression is evaluated first?

1.
x ** 2 + 6 / 3
  1. 6 / 3
  2. x ** 2
  3. 2 + 6
2.
not 3 * 5 > 10
  1. 3 * 5
  2. not 3
  3. 5 > 10
3.
z == 5 and x / 8 < 100
  1. 5 and x
  2. x / 8
  3. 8 < 100

Associativity

What if operators beside each other have the same level of precedence? Associativity determines the order of operations when precedence is the same. Ex: 8 / 4 * 3 is evaluated as (8/4) * 3 rather than 8 / (4*3) because multiplication and division are left associative. Most operators are left associative and are evaluated from left to right. Exponentiation is the main exception (noted above) and is right associative: that is, evaluated from right to left. Ex: 2 ** 3 ** 4 is evaluated as 2 ** (3**4).

When comparison operators are chained, the expression is converted into the equivalent combination of comparisons and evaluated from left to right. Ex. 10 < x <= 20 is evaluated as 10 < x and x <= 20.

Checkpoint

Concepts in Practice

Associativity

How is each expression evaluated?

4.
10 + 3 * 2 / 4
  1. 10 + (3 * (2 / 4))
  2. 10 + ((3 * 2) / 4)
  3. (10 + 3) * (2 / 4)
5.
2 * 2 ** 2 ** 3
  1. 2 * ((2 ** 2) ** 3)
  2. 2 * (2 ** (2 ** 3))
  3. ((2*2) ** 2) ** 3
6.
100 < x > 150
  1. 100 < x and x < 150
  2. 100 < x or x > 150
  3. 100 < x and x > 150

Enforcing order and clarity with parentheses

Operator precedence rules can be hard to remember. Parentheses not only assert a different order of operations but also reduce confusion.

Checkpoint

Concepts in Practice

Using parentheses

7.
Consider the example above. Why was the evaluation order different from what the programmer wanted?
  1. Equality has precedence over and.
  2. All operators are evaluated right to left.
  3. Order is random when parentheses aren't used.
8.
Given x = 8 and y = 9, what is the result of the following?
x + 3 * y - 5
  1. 30
  2. 44
  3. 94
9.
Given x = 8 and y = 9, what is the result of the following?
(x+3) * (y-5)
  1. 30
  2. 44
  3. 94

PEP 8 recommendations: spacing around operators

The PEP 8 style guide recommends consistent spacing around operators to avoid extraneous and confusing whitespace.

  • Avoid multiple spaces and an unequal amount of whitespace around operators with two operands.
    Avoid: x= y * 44
    Better: x = y * 44
  • Avoid spaces immediately inside parentheses.
    Avoid: x = ( 4 * y )
    Better: x = (4 * y)
  • Surround the following operators with one space: assignment, augment assignment, comparison, Boolean.
    Avoid: x= y<44
    Better: x = y < 44
  • Consider adding whitespace around operators with lower priority.
    Avoid: x = 5 * z+20
    Better: x = 5*z + 20
Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.