Operator Precedence

suggest change

Introduction

Python operators have a set order of precedence, which determines what operators are evaluated first in a potentially ambiguous expression. For instance, in the expression 3 * 2 + 7, first 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.

Remarks

From the Python documentation:

The following table summarizes the operator precedences in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right and exponentiation, which groups from right to left).

Operator | Description | —— | —— | lambda | Lambda expression| if – else | Conditional expression| or | Boolean OR| and | Boolean AND| not x | Boolean NOT| in, not in, is, is not, <, <=, >, >=, <>, !=, == | Comparisons, including membership tests and identity tests| | | Bitwise OR| | ^ | Bitwise XOR| & | Bitwise AND| <<, >> | Shifts| +, - | Addition and subtraction| *, /, //, % | Multiplication, division, remainder [8]| +x, -x, ~x | Positive, negative, bitwise NOT| ** | Exponentiation [9]| x[index], x[index:index], x(arguments…), x.attribute | Subscription, slicing, call, attribute reference| (expressions…), [expressions…], {key: value…}, expressions… | Binding or tuple display, list display, dictionary display, string conversion|

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Operator Precedence:
* Operator Precedence

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
127 Operator Precedence
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve