Python教程-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