📜  Python中的赋值运算符

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

Python中的赋值运算符

运算符用于对值和变量执行操作。这些是执行算术、逻辑、按位计算的特殊符号。运算符操作的值称为Operand

在这里,我们将介绍Python中的赋值运算符。因此,赋值运算符用于为变量赋值。

Operator

Description

Syntax

=

Assign value of right side of expression to left side operandx = y + z 

+=

Add and Assign: Add right side operand with left side operand and then assign to left operanda += b   

-=

Subtract AND: Subtract right operand from left operand and then assign to left operand: True if both operands are equala -= b  

*=

Multiply AND: Multiply right operand with left operand and then assign to left operanda *= b     

/=

Divide AND: Divide left operand with right operand and then assign to left operanda /= b

%=

Modulus AND: Takes modulus using left and right operands and assign result to left operanda %= b  

//=

Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operanda //= b   

**=

Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operanda **= b     

&=

Performs Bitwise AND on operands and assign value to left operanda &= b   

|=

Performs Bitwise OR on operands and assign value to left operanda |= b    

^=

Performs Bitwise xOR on operands and assign value to left operanda ^= b    

>>=

Performs Bitwise right shift on operands and assign value to left operanda >>= b     

<<=

Performs Bitwise left shift on operands and assign value to left operanda <<= b 

现在让我们一一查看每个赋值运算符。

1) 赋值:该运算符用于将表达式右边的值赋给左边的操作数。

句法:

x = y + z

例子:

Python3
# Assigning values using 
# Assignment Operator
  
a = 3
b = 5
  
c = a + b
  
# Output
print(c)


Python3
a = 3
b = 5
  
# a = a + b
a += b
  
# Output
print(a)


Python3
a = 3
b = 5
  
# a = a - b
a -= b
  
# Output
print(a)


Python3
a = 3
b = 5
  
# a = a * b
a *= b
  
# Output
print(a)


Python3
a = 3
b = 5
  
# a = a / b
a /= b
  
# Output
print(a)


Python3
a = 3
b = 5
  
# a = a % b
a %= b
  
# Output
print(a)


Python
a = 3
b = 5
  
# a = a // b
a //= b
  
# Output
print(a)


Python
a = 3
b = 5
  
# a = a ** b
a **= b
  
# Output
print(a)


Python3
a = 3
b = 5
  
# a = a & b
a &= b
  
# Output
print(a)


Python3
a = 3
b = 5
  
# a = a | b
a |= b
  
# Output
print(a)


Python3
a = 3
b = 5
  
# a = a ^ b
a ^= b
  
# Output
print(a)


Python3
a = 3
b = 5
  
# a = a >> b
a >>= b
  
# Output
print(a)


Python3
a = 3
b = 5
  
# a = a << b
a <<= b
  
# Output
print(a)


输出:

8

2) Add and Assign:该运算符用于将右侧操作数与左侧操作数相加,然后将结果分配给左侧操作数。

句法:

x += y

例子:

Python3

a = 3
b = 5
  
# a = a + b
a += b
  
# Output
print(a)

输出:

8

3)减法赋值:该运算符用于从左操作数中减去右操作数,然后将结果赋给左操作数。

句法:

x -= y

例子 -

Python3

a = 3
b = 5
  
# a = a - b
a -= b
  
# Output
print(a)

输出:

-2

4)乘法和赋值:该运算符用于将右操作数与左操作数相乘,然后将结果分配给左操作数。

句法:

x *= y

例子:

Python3

a = 3
b = 5
  
# a = a * b
a *= b
  
# Output
print(a)

输出:

15

5)除并赋值:该运算符用于将左操作数与右操作数相除,然后将结果分配给左操作数。

句法:

x /= y

例子:

Python3

a = 3
b = 5
  
# a = a / b
a /= b
  
# Output
print(a)

输出:

0.6

6)模数和赋值:该运算符用于使用左右操作数取模,然后将结果分配给左操作数。

句法:

x %= y

例子:

Python3

a = 3
b = 5
  
# a = a % b
a %= b
  
# Output
print(a)

输出:

3

7)除(地板)和赋值:该运算符用于将左操作数与右操作数相除,然后将结果(地板)分配给左操作数。

句法:

x //= y

例子:

Python

a = 3
b = 5
  
# a = a // b
a //= b
  
# Output
print(a)

输出:

0

8)指数和赋值:该运算符用于使用操作数计算指数(升幂)值,然后将结果分配给左操作数。

句法:

x **= y

例子:

Python

a = 3
b = 5
  
# a = a ** b
a **= b
  
# Output
print(a)

输出:

243

9) 按位与和赋值:该运算符用于对两个操作数进行按位与,然后将结果分配给左操作数。

句法:

x &= y

例子:

Python3

a = 3
b = 5
  
# a = a & b
a &= b
  
# Output
print(a)

输出:

1

10)按位或赋值:该运算符用于对操作数进行按位或,然后将结果分配给左操作数。

句法:

x |= y

例子:

Python3

a = 3
b = 5
  
# a = a | b
a |= b
  
# Output
print(a)

输出:

7

11)按位异或并赋值:该运算符用于对操作数进行按位异或,然后将结果赋给左操作数。

句法:

x ^= y

例子:

Python3

a = 3
b = 5
  
# a = a ^ b
a ^= b
  
# Output
print(a)

输出:

6

12)按位右移和赋值:该运算符用于对操作数进行按位右移,然后将结果赋给左操作数。

句法:

x >>= y

例子:

Python3

a = 3
b = 5
  
# a = a >> b
a >>= b
  
# Output
print(a)

输出:

0

13)按位左移和赋值:该运算符用于对操作数进行按位左移,然后将结果赋给左操作数。

句法:

x <<= y

例子:

Python3

a = 3
b = 5
  
# a = a << b
a <<= b
  
# Output
print(a)

输出:

96