##### Python Programming Tutorial

- 1: Getting started with Python Language – Part 1
- 1: Getting started with Python Language – Part 2
- 2: Python Data Types
- 3: Indentation in Python
- 4: Comments and Documentation in Python
- 5: Date and Time in Python
- 6: Date Formatting in Python
- 7: Enum in Python
- 8: Set in Python
- 9: Simple Mathematical Operators in Python
- 10: Bitwise Operators in Python
- 11: Boolean Operators in Python
- 12: Operator Precedence in Python
- 13: Variable Scope and Binding in Python
- 14: Conditionals statement in python
- 15: Comparisons operators in python
- 16: Loops in python
- 17: Arrays in python
- 18: Multidimensional arrays in Python
- 19: Dictionary in Python
- 20: List in Python
- 21: List comprehensions in Python
- 22: List slicing (selecting parts of lists) in Python
- 23: groupby() clause in Python
- 24: Linked lists in Python
- 25: Linked List Node in Python
- 26: Filter in Python
- 27: Heapq in Python
- 28: Tuple in Python
- 29: Basic Input and Output in Python
- 30: Files & Folders I/O in Python
- 31: os.path in Python

12. Operator Precedence

Python operators have a set order of precedence, which determines what operators are evaluated ﬁrst in a potentially ambiguous expression. For instance, in the expression 3 * 2 + 7, ﬁrst 3 is multiplied by 2, and then the result is added to 7, yielding 13. The expression is not evaluated the other way around, because * has a higher precedence than +.

Below is a list of operators by precedence, and a brief description of what they (usually) do.

Section 12.1: Simple Operator Precedence Examples in python

Python follows PEMDAS rule. PEMDAS stands for Parentheses, Exponents, Multiplication and Division, and Addition and Subtraction.

Example:

>>>a, b, c, d = 2, 3, 5, 7

>>>a ** (b + c) # parentheses

256

>>>a * b ** c # exponent: same as `a * (b ** c)`

7776

>>>a + b * c / d # multiplication / division: same as `a + (b * c / d)` 4.142857142857142

Extras: mathematical rules hold, but not always:

>>>300 / 300 * 200

200.0

>>>300 * 200 / 300

200.0

>>>1e300 / 1e300 * 1e200

1e+200

>>>1e300 * 1e200 / 1e300

inf

*This content is compiled from Stack Overﬂow Documentation, and the content is written by the beautiful people at Stack Overﬂow.

*This content is compiled from Stack Overﬂow Documentation, and the content is written by the beautiful people at Stack Overﬂow. This work is licensed under cc by-sa.