Python Operator Precedence – Learn how to perform operations in Python
Python Operator Precedence – Learn how to perform operations in Python
here are many different types of operators. When evaluating complex expressions like 5+2*4%6-1 and 13 or 3 one might easily get confused about in which order the operations will be performed.
This Python operator precedence article will help you in understanding how these expressions are evaluated and the order of precedence Python follows.
Python Operators Precedence Table
Here we have a table that is arranged in the ascending order of precedence of operators.
The new Assignment expression (:=) operator from Python 3.8 onwards has the lowest precedence while parentheses() have the highest precedence.
Operator | Description |
:= | Assignment expression (Lowest precedence) |
lambda | Lambda expression |
if-else | Conditional expression |
or | Boolean OR |
and | Boolean AND |
not x | Boolean NOT |
<, <=, >, >=, | Comparison operators |
!=, == | Equality operators |
in, not in, is, is not, | Identity operators, membership operators |
| | Bitwise OR |
^ | Bitwise XOR |
& | Bitwise AND |
<<, >> | Left and right Shifts |
+, – | Addition and subtraction |
*, @, /, //, % | Multiplication, matrix multiplication, division, floor division, remainder |
+x, -x, ~x | Unary plus, Unary minus, bitwise NOT |
** | Exponentiation |
await x | Await expression |
x[index], x[index:index], x(arguments…), x.attribute | Subscription, slicing, call, attribute reference |
(expressions…), [expressions…],
{key: value…}, {expressions…} |
Binding or parenthesized expression, list display, dictionary display, set display |
() | Parentheses (Highest precedence) |
Python Operators Precedence Rule – PEMDAS
You might have heard about the BODMAS rule in your school’s mathematics class. Python also uses a similar type of rule known as PEMDAS.
P – Parentheses
E – Exponentiation
M – Multiplication
D – Division
A – Addition
S – Subtraction
The precedence of operators is listed from High to low. To remember the abbreviations, we have a funny mnemonic “Please Excuse My Dear Aunt Sally”.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-09-24 Anti forgery token is meant for user "" but the current user is "username"
2021-09-24 Asp.net Mvc AntiForgeryToken全局处理
2021-09-24 Does it make sense to put antiforgerytoken in _Layout.cshtml?
2021-09-24 jQuery .val()
2021-09-24 Understanding multiple anti-forgery tokens in ASP.NET MVC
2021-09-24 Duplicate Files Search & Link
2021-09-24 The source contains no DataRows