Python中的赋值运算符
在本节中,我们将讨论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中不同类型的赋值运算符:
- 简单赋值运算符(
=
) - 加等赋值运算符(
+=
) - 减等赋值运算符(
-=
) - 乘等赋值运算符(
*=
) - 除等赋值运算符(
/=
) - 模等赋值运算符(
%=
) - 双除等赋值运算符(
//=
) - 指数赋值运算符(
**=
) - 位与赋值运算符(
&=
) - 位或赋值运算符(
|=
) - 位异或赋值运算符(
^=
) - 位右移赋值运算符(
>>=
) - 位左移赋值运算符(
<<=
)
赋值运算符(=
)
简单赋值运算符将右边的操作数表达式或值赋给左边的操作数。
语法
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
按位与(&)和赋值运算符(&=
)
按位与(&)和赋值运算符用于对左右操作数进行操作,并将结果赋值给左操作数。
语法
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