📜  Python中运算符的优先级和关联性

📅  最后修改于: 2022-05-13 01:55:42.495000             🧑  作者: Mango

Python中运算符的优先级和关联性

在Python中处理运算符时,我们必须了解Python运算符优先级和关联性的概念,因为它们决定了运算符的优先级,否则,我们将看到意外的输出。

运算符优先级:这用于具有多个具有不同优先级的运算符的表达式中,以确定首先执行哪个操作。

示例:求解

10 + 20 * 30

10 + 20 * 30 is calculated as 10 + (20 * 30)
and not as (10 + 20) * 30

代码:

Python3
# Precedence of '+' & '*'
expr = 10 + 20 * 30
  
print(expr)


Python3
# Precedence of 'or' & 'and'
name = "Alex"
age = 0
  
if name == "Alex" or name == "John" and age >= 2 :
    print("Hello! Welcome.")
else :
    print("Good Bye!!")


Python3
# Precedence of 'or' & 'and'
name = "Alex"
age = 0
  
if ( name == "Alex" or name == "John" ) and age >= 2 :
  print("Hello! Welcome.")
else :
  print("Good Bye!!")


Python3
# Left-right associativity
# 100 / 10 * 10 is calculated as 
# (100 / 10) * 10 and not 
# as 100 / (10 * 10)
print(100 / 10 * 10)
  
# Left-right associativity
# 5 - 2 + 3 is calculated as 
# (5 - 2) + 3 and not 
# as 5 - (2 + 3)
print(5 - 2 + 3)
  
# left-right associativity
print(5 - (2 + 3))
  
# right-left associativity
# 2 ** 3 ** 2 is calculated as 
# 2 ** (3 ** 2) and not 
# as (2 ** 3) ** 2
print(2 ** 3 ** 2)


Python3
expression  = 100 + 200 / 10 - 3 * 10
print(expression )


输出:

610

示例:现在,让我们看一个关于逻辑'' &逻辑''运算符的示例。 ' if ' 块即使年龄为 0 也会执行。因为逻辑 '' 的优先级大于逻辑 ''。

Python3

# Precedence of 'or' & 'and'
name = "Alex"
age = 0
  
if name == "Alex" or name == "John" and age >= 2 :
    print("Hello! Welcome.")
else :
    print("Good Bye!!")

输出:

Hello! Welcome.

因此,要运行“ else ”块,我们可以使用括号() ,因为它们的优先级在所有运算符中最高。

Python3

# Precedence of 'or' & 'and'
name = "Alex"
age = 0
  
if ( name == "Alex" or name == "John" ) and age >= 2 :
  print("Hello! Welcome.")
else :
  print("Good Bye!!")

输出:

Good Bye!!

运算符关联性:如果一个表达式包含两个或多个具有相同优先级的运算符符,则使用运算符关联性来确定。它可以是从左到右从右到左

示例: '*' 和 '/' 具有相同的优先级,并且它们的关联性是从左到右,因此表达式“100 / 10 * 10”被视为“(100 / 10) * 10”。

代码:

Python3

# Left-right associativity
# 100 / 10 * 10 is calculated as 
# (100 / 10) * 10 and not 
# as 100 / (10 * 10)
print(100 / 10 * 10)
  
# Left-right associativity
# 5 - 2 + 3 is calculated as 
# (5 - 2) + 3 and not 
# as 5 - (2 + 3)
print(5 - 2 + 3)
  
# left-right associativity
print(5 - (2 + 3))
  
# right-left associativity
# 2 ** 3 ** 2 is calculated as 
# 2 ** (3 ** 2) and not 
# as (2 ** 3) ** 2
print(2 ** 3 ** 2)

输出:

100
6
0
512

示例:求解

100 + 200 / 10 - 3 * 10

100 + 200 / 10 - 3 * 10 is calculated as 100 + (200 / 10) - (3 * 10)
and not as (100 + 200) / (10 - 3) * 10

代码:

Python3

expression  = 100 + 200 / 10 - 3 * 10
print(expression )

输出:

90.0

请参阅以下优先级和关联性表以供参考。此表列出了从最高优先级到最低优先级的所有运算符。

OperatorDescription  Associativity
( )Parentheses  left-to-right
**Exponent right-to-left
*  /  %Multiplication/division/modulusleft-to-right
+  –Addition/subtractionleft-to-right
<<  >>Bitwise shift left, Bitwise shift rightleft-to-right
<  <= 
>  >=
Relational less than/less than or equal to 
Relational greater than/greater  than or equal to
left-to-right
==  !=Relational is equal to/is not equal toleft-to-right

is,  is not

in, not in

Identity

Membership operators

left-to-right
&Bitwise ANDleft-to-right
^Bitwise exclusive ORleft-to-right
|Bitwise inclusive ORleft-to-right
notLogical NOTright-to-left
andLogical ANDleft-to-right
orLogical ORleft-to-right

+=  -= 
*=  /= 
%=  &= 
^=  |= 
<<=  >>=
Assignment 
Addition/subtraction assignment 
Multiplication/division assignment 
Modulus/bitwise AND assignment 
Bitwise exclusive/inclusive OR assignment 
Bitwise shift left/right assignment
right-to-left