Python 赋值运算符
Python =(等于)符号定义为赋值运算符(Assignment Operator)。其右侧 表达式的值将分配给其左侧的单个变量。通常编程(特别是 Python)中的 = 符号不应与它在数学中的用法相混淆,在数学中,它表示符号两侧的表达式相等。
Python 赋值运算符示例
请考虑遵循 Python 语句 -
a = 10
b = 5
a = a + b
print (a)
乍一看,至少对于刚接触编程但懂数学的人来说,“a=a+b”这个语句看起来很奇怪。a 怎么可能等于 “a+b”?但是,需要再次强调的是,= 符号在这里是一个赋值运算符,不用于表示 LHS 和 RHS 的相等性。
因为它是一个赋值,所以右侧的表达式的计算结果为 15,该值被分配给 a。
在语句 “a+=b” 中,运算符 “+” 和 “=” 可以组合成 “+=” 算子。它被称为 add and assign 运算符。在单个语句中,它执行两个操作数 “a” 和 “b” 的加法,并将结果分配给左侧的操作数,即 “a”。
Python 增强赋值运算符
除了简单的赋值运算符之外,Python 还提供了一些额外的赋值运算符供高级使用。它们称为累积或增强的赋值运算符。在本章中,我们将学习使用 Python 定义的增强赋值运算符。
Python 增强赋值运算符将加法和赋值组合在一个语句中。由于 Python 支持混合算术,因此这两个操作数可能属于不同的类型。但是,如果 left 操作数的类型较宽,则 Left 操作数类型将更改为 Right 操作数。
示例
+= 运算符是增强的运算符。它也称为累积加法运算符,因为它在 “a” 中添加了 “b” 并将结果分配回变量。
以下是 Python 增强赋值运算符:
- 增强加法运算符
- 增强减法运算符
- 增强乘法运算符
- 增强除法运算符
- 增强余数运算符
- 增强指数运算符
- 增强地板除法运算符
以下示例将有助于理解 “+= 运算符” 的工作原理 -
a=10
b=5
print ("int和int的增强加法")
a+=b # 相当于a=a+b
print ("a=",a, "type(a):", type(a))
a=10
b=5.5
print ("int和float的增强加法")
a+=b # 相当于a=a+b
print ("a=",a, "type(a):", type(a))
a=10.50
b=5+6j
print ("增强浮点数和复数")
a+=b # 相当于a=a+b
print ("a=",a, "type(a):", type(a))
它将产生以下输出 -
a= 15 type(a): <class 'int'>
int和float的增强加法
a= 15.5 type(a): <class 'float'>
增强浮点数和复数
a= (15.5+6j) type(a): <class 'complex'>
增强减法运算符 (-=)
使用 -= 符号在单个语句中执行减法和赋值运算。“a-=b” 语句执行 “a=a-b” 赋值。操作数可以是任何数字类型。Python 对大小较窄 对象 执行隐式类型转换。
a=10
b=5
print ("int和int的增强减法")
a-=b # 相当于a=a-b
print ("a=",a, "type(a):", type(a))
a=10
b=5.5
print ("int和float的增强减法")
a-=b # 相当于a=a-b
print ("a=",a, "type(a):", type(a))
a=10.50
b=5+6j
print ("浮点数和复数的增强减法")
a-=b # 相当于a=a-b
print ("a=",a, "type(a):", type(a))
它将产生以下输出 -
a= 5 type(a): <class 'int'>
int和float的增强减法
a= 4.5 type(a): <class 'float'>
浮点数和复数的增强减法
a= (5.5-6j) type(a): <class 'complex'>
增强乘法运算符 (*=)
“*=”运算符的工作原理类似。“a*=b” 执行乘法和赋值运算,相当于 “a=a*b”。在两个复数的增倍乘法的情况下,上一章讨论的乘法规则适用。
a=10
b=5
print ("int和int的增强乘法")
a*=b # 相当于a=a*b
print ("a=",a, "type(a):", type(a))
a=10
b=5.5
print ("int和float的增强乘法")
a*=b # 相当于a=a*b
print ("a=",a, "type(a):", type(a))
a=6+4j
b=3+2j
print ("complex和complex的增强乘法")
a*=b # 相当于a=a*b
print ("a=",a, "type(a):", type(a))
它将产生以下输出 -
a= 50 type(a): <class 'int'>
int和float的增强乘法
a= 55.0 type(a): <class 'float'>
complex和complex的增强乘法
a= (10+24j) type(a): <class 'complex'>
增强除法运算符 (/=)
组合符号 “/=” 充当除法和赋值运算符,因此 “a/=b” 等同于 “a=a/b”。int 或 float 操作数的除法运算是 float。将两个复数相除将返回一个复数。下面给出了增强除法运算符的示例。
a=10 b=5 print ("int和int的增强除法") a/=b # 相当于a=a/b print ("a=",a, "type(a):", type(a)) a=10 b=5.5 print ("int和float的增强除法") a/=b # 相当于a=a/b print ("a=",a, "type(a):", type(a)) a=6+4j b=3+2j print ("
complex和complex的增强除法") a/=b # 相当于a=a/b print ("a=",a, "type(a):", type(a))
它将产生以下输出 -
a= 2.0 type(a): <class 'float'>
int和float的增强除法
a= 1.8181818181818181 type(a): <class 'float'>
complex和complex的增强除法
a= (2+0j) type(a): <class 'complex'>
增强模数运算符 (%=)
要在单个语句中执行模数和赋值运算,请使用 %= 运算符。与 mod 运算符一样,其 augmented 版本也不支持复数。
a=10
b=5
print ("具有int和int的增强模运算符")
a%=b # 相当于a=a%b
print ("a=",a, "type(a):", type(a))
a=10
b=5.5
print ("带int和float的增强模运算符")
a%=b # 相当于a=a%b
print ("a=",a, "type(a):", type(a))
它将产生以下输出 -
a= 0 type(a): <class 'int'>
带int和float的增强模运算符
a= 4.5 type(a): <class 'float'>
增强指数运算符 (**=)
“**=”运算符导致将 “a” 的计算提高到 “b”,并将值分配回 “a”。下面给出了一些示例——
a=10
b=5
print ("带int和int的增强指数运算符")
a**=b # 相当于a=a**b
print ("a=",a, "type(a):", type(a))
a=10
b=5.5
print ("带int和float的增强指数运算符")
a**=b # 相当于a=a**b
print ("a=",a, "type(a):", type(a))
a=6+4j
b=3+2j
print ("complex和complex增强指数运算符")
a**=b # 相当于a=a**b
print ("a=",a, "type(a):", type(a))
它将产生以下输出 -
a= 100000 type(a): <class 'int'>
带int和float的增强指数运算符
a= 316227.7660168379 type(a): <class 'float'>
complex和complex增强指数运算符
a= (97.52306038414744-62.22529992036203j) type(a): <class 'complex'>
增强地板除法运算符 (//=)
要在单个语句中执行 floor 除法和赋值,请使用 “//=” 运算符。“a//=b” 等同于 “a=a//b”。此运算符不能与复数一起使用。
a=10
b=5
print ("带int和int的增强地板除运算符")
a//=b # 相当于a=a//b
print ("a=",a, "type(a):", type(a))
a=10
b=5.5
print ("带int和float的增强地板除运算符")
a//=b # 相当于a=a//b
print ("a=",a, "type(a):", type(a))
它将产生以下输出 -
a= 2 type(a): <class 'int'>
带int和float的增强地板除运算符
a= 1.0 type(a): <class 'float'>