Home > Backend Development > Python Tutorial > Master the order of precedence of Python operators and gain insight into your opponents

Master the order of precedence of Python operators and gain insight into your opponents

PHPz
Release: 2024-01-20 08:50:06
Original
1291 people have browsed it

Master the order of precedence of Python operators and gain insight into your opponents

Know yourself and your enemy. Only by understanding the priority order of Python operators can you write better code. This article will introduce Python operator precedence starting from basic concepts and deepen understanding through specific code examples.

The concept of Python operator precedence is similar to operator precedence in mathematics. In an expression, operators have different priorities, and operators with higher precedence are evaluated before operators with lower precedence. Failure to understand operator precedence may result in expressions that do not evaluate as expected.

First, we need to know the classification of Python operators. Python's operators are mainly divided into arithmetic operators, comparison operators, assignment operators, logical operators and bitwise operators. Each type of operator has a specific precedence.

The precedence of arithmetic operators from high to low is: exponent operator (*), plus and minus sign ( , -), multiplication and division (, /, //, %), addition and subtraction ( , -).

The following is a sample code showing the application of arithmetic operator precedence:

a = 2 + 3 * 4
print(a)  # 输出结果为14,先计算乘法再加法

b = (2 + 3) * 4
print(b)  # 输出结果为20,先计算括号里的加法再乘法

c = 2 ** 3 + 4
print(c)  # 输出结果为12,先计算指数运算再加法

d = -2 * 3
print(d)  # 输出结果为-6,先计算负号再乘法
Copy after login

Comparison operators have higher precedence, and their precedence is as follows: Less than (< ), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (!=).

The following is an example of the priority of comparison operators:

e = 4 > 3 + 1
print(e)  # 输出结果为False,先计算加法再比较大小

f = 2 + 3 != 5
print(f)  # 输出结果为False,先计算加法再比较大小再判断不等于

g = 5 == 2 * 2 + 1
print(g)  # 输出结果为True,先计算乘法再加法再比较大小再判断等于
Copy after login

The priority of assignment operators is relatively low, and their priorities from right to left are: assignment operators ( =), incremental assignment operators (=, -=, =, /=, //=, %=, *=).

Let’s look at an example of assignment operator priority:

h = i = j = 1 + 2
print(h, i, j)  # 输出结果为3,分别是3,3,3,从右往左赋值

k = 5
k += 2 * 3
print(k)  # 输出结果为11,先计算乘法再加法再增量赋值
Copy after login

The priority of logical operators from high to low is: not (not), and (and), or (or ).

The following is an example of the priority of logical operators:

l = True or False and not True
print(l)  # 输出结果为True,先计算not再and再or

m = (True or False) and not True
print(m)  # 输出结果为False,先计算括号里的or再and再not
Copy after login

The last is the priority of bit operators. The priority of bit operators from high to low is: bitwise negation (~), bitwise AND (&), bitwise OR (|), bitwise XOR (^), left shift (<<) and right shift (>>).

The following example shows the application of bit operator precedence:

n = 3 | 4 ^ 5 & ~6
print(n)  # 输出结果为2,顺序为5&~6=4、4^4=0、3|0=3

o = 8 << 2 + 1
print(o)  # 输出结果为64,先计算加法再左移

p = 8 >> 2 + 1
print(p)  # 输出结果为1,先计算加法再右移
Copy after login

Through the above example, we can clearly understand the precedence order of Python operators. Accurately grasping operator precedence is very important for writing and reading code, and can avoid many potential errors. I hope this article can help readers have a deeper understanding of Python operator precedence and be able to apply it flexibly in practice.

The above is the detailed content of Master the order of precedence of Python operators and gain insight into your opponents. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template