首页 > 编程知识 正文

Python 中的赋值运算符

时间:2023-11-26 13:13:05 阅读:309314 作者:TXMS

在本节中,我们将讨论 Python 编程语言中的赋值运算符。在进入主题之前,让我们简单介绍一下 Python 中的运算符。运算符是在编程语言中用于操作数之间执行逻辑和数学运算的特殊符号。操作员对其进行运算的值称为操作数。有不同类型的运算符算术、逻辑、关系、赋值和按位等。

Python 有一个赋值运算符,有助于为左侧变量赋值或表达式。赋值运算符表示为赋值语句和赋值表达式中使用的“=”符号。在赋值运算符中,右侧的值或操作数被赋给左侧的操作数。

以下是赋值运算符的示例:


x = 8                  # here 8 is assigned to the x (left side operand)
y = 20                  # here 8 is assigned to the x (left side operand)
c = a + b - 5            # here the value of expression a + b - 5 is assigned to c

赋值运算符的类型

以下是 Python 中不同类型的赋值运算符:

  1. 简单赋值运算符(=)
  2. 加法和相等运算符(+=)
  3. 加减运算符(-=)
  4. 星号和等号运算符(*=)
  5. 除与等运算符(/=)
  6. 模数和相等运算符(%=)
  7. 双除等运算符(//=)
  8. 指数赋值运算符(**=)
  9. 按位“与”运算符(&=)
  10. 按位或运算符(|=)
  11. 按位异或赋值运算符(^=)
  12. 按位右移赋值运算符(> > =)
  13. 按位左移赋值运算符(< < =)

赋值运算符(=)

简单赋值运算符将右侧操作数表达式或值赋给左侧操作数。

语法


C = A + B

示例:


# Program to demonstrate the simple Assignment Operator.
a = 5 # assign value
b = a # assign the expression to the left operand
# print result
print( "Output = ", b)

输出:

Output = 5

加法和赋值运算符(+=)

运算符将右侧操作数或值添加到左侧操作数,然后将结果赋给左侧操作数。

语法


A += B or A = A + B

示例:


# Program to demonstrate the Add and Assignment Operators in Python. 
a = 5 # assign value
b = 3
# a = a + b assign the expression to the left operand 
a += b 
# print result
print( "Output = ", a)

输出:

Output = 9

减法和赋值运算符(-=)

运算符从左侧操作数中减去右侧操作数或值,并将该值存储到左侧操作数中。

语法


C -= A or C = C - A

示例:


# Program to demonstrate the Subtract and Assign Operators in Python. 
a = 5 # assign value
b = 3
# a = a - b or a -= b assign the expression to the left operand 
a -= b 
# print result
print( "Output = ", a)

输出:

Output = 2

乘法和赋值运算符(*=)

运算符将右侧操作数或值乘以左侧操作数,并将乘积存储到左侧操作数。

语法


A *= B or A = A * B

示例:


# Program to demonstrate the Multiply and Assign Operators in Python. 
a = 15 # assign value
b = 4
# a = a * b, or a *= b assign the expression to the left operand 
a *= b 
# print result
print( "Output = ", a)

输出:

Output = 60

除法和赋值运算符(/=)

运算符将左操作数除以右操作数,然后将结果赋给左操作数。

语法


B /= A or B = B / A

示例:


# Program to demonstrate the Divide and Assign Operators in Python. 
a = 80 # assign value
b = 4
# a = a / b or a /= b assign the expression to the left operand 
a /= b 
# print result
print( "Output = ", a)

输出:

Output = 20.0

模数和赋值运算符(%=)

运算符将左侧操作数除以右侧操作数或值,并将余数放在左侧操作数上。

语法


B %= A or B = B % A

示例:


# Program to demonstrate the Modulus and Assign Operators in Python. 
a = 80 # assign value
b = 6
# a = a % b or a %= b assign the expression to the left operand 
a %= b 
# print result
print( "Output = ", a)

输出:

Output = 2

楼层划分和分配运算符(//=)

底板除法运算符将左侧操作数除以右侧操作数或值,然后将底板(值)赋给左侧操作数。

语法


B //= A or B = B // A

示例:


# Program to demonstrate the Floor division and Assign Operators in Python. 
a = 131 # assign value
b = 6
# a = a // b or a //= b assign the expression to the left operand 
a //= b 
# print result
print( "Output = ", a)

输出:

Output = 21

指数和赋值运算符(**=)

指数赋值运算符用于使用两个操作数获取指数值,并将结果赋给左操作数。

语法


B **= A or B = B ** A

示例:


# Program to demonstrate the exponent (**) and Assign Operators in Python.
a = 4 # assign value
b = 3
# a = a ** b or a **= b assign the expression to the left operand 
a **= b 
# print result
print( "Output = ", a)

输出:

Output = 64

按位 And (&)和赋值运算符(&=)

按位 And (&)和赋值运算符用于对(左和右)操作数进行运算,并将结果赋给左操作数。

语法


B &= A

示例:


# Program to demonstrate the Bitwise And (&) and Assign Operators in Python. 
a = 6 # assign value
b = 13 
#   0110  (6 in binary)
# & 1101  (13 in binary)
#  ________
#   0100 = 4 (In decimal)

# a = a & b or a &= b assign the expression value to the left operand 
a &= b   
# print result
print( "Output = ", a)

输出:

Output = 4

按位或和赋值运算符(|=)

按位“或”和“赋值”运算符用于对(左和右)操作数进行运算,并将结果存储到左操作数中。

语法


B |= A

示例:


# Program to demonstrate the Bitwise OR and Assign Operators in Python. 
a = 6 # assign value
b = 13 
#   0110  (6 in binary)
# | 1101  (13 in binary)
#  ________
#   0100 = 4 (In decimal)

# a = a | b or a |= b assign the expression values to the left operand 
a |= b   
# print result
print( "Output = ", a)

输出:

Output = 15

按位异或和赋值运算符(^=)

按位异或和赋值运算符对(左和右)操作数进行运算,并将结果赋给左操作数。

语法


B ^= A

示例:


# Program to demonstrate the Bitwise XOR and Assign Operators in Python. 
a = 6 # assign value
b = 13 

# a = a | b or a |= b assign the expression values to the left operand 
a ^= b   
# print result
print( "Output = ", a)

输出:

Output = 11

按位右移和赋值运算符(> > =)

运算符将指定数量的位或操作数向右移动,并将值赋给左操作数。

语法


B >>= A

示例:


# Program to demonstrate the Bitwise Right shift and Assign Operators in Python. 
a = 6 # assign value
b = 2 

# a = a >> b or a >>= b assign the expression value to the left operand 
a >>= b   
# print result
print( "Output = ", a)

输出:

Output = 1

按位左移和赋值运算符(< < =)

运算符将指定数量的操作数向左移动,并将结果赋给左操作数。

语法


B <<= A

示例:


# Program to demonstrate the Bitwise Left shift and Assign Operators in Python. 
a = 6 # assign value
b = 2 

# a = a >> b or a >>= b, assign the expression value to the left operand 
a <<= b   
# print result
print( "Output = ", a)  

输出:

Output = 24

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。