Python 简明教程

Python - Assignment Operators

Python Assignment Operator

=(等于)符号被定义为 Python 中的赋值运算符。其右侧的 Python 表达式值被分配给其左侧的单个 variable 。与一般的编程语言(及其 Python 语言)中一样,符号 = 不应与其在数学中的用法混淆,在数学中,它表示符号两侧的表达式相等。

Example of Assignment Operator in Python

考虑下面的 Python 语句:

a = 10
b = 5
a = a + b
print (a)

一开始,对于编程新手而言,即使他们懂数学,“a=a+b”这条语句看起来也很奇怪。a 怎么可能等于“a+b”?然而,需要注意的是,符号在这里是一个赋值运算符,而不是用来表示左右两侧相等。

因为它是一个赋值,所以右侧表达式求值为 15,该值被分配给 a。

在语句“a+=b”中,两个运算符“" 和 "=" 可以组合成一个“=" 运算符。这称为加法和赋值运算符。在一条语句中,它对两个操作数“a”和“b”执行加法运算,并将结果分配给左侧操作数, 即“a”。

Augmented Assignment Operators in Python

除了简单的赋值运算符外,Python 还提供了更多可用于高级用途的赋值运算符。它们称为累积赋值运算符或扩充赋值运算符。在本章中,我们将学习如何使用 Python 中定义的扩充赋值运算符。

Python 具有所有 arithmeticcomparison 运算符的增强赋值运算符。

Python 增强赋值运算符在一个语句中结合了加法和赋值。由于 Python 支持混合算术,因此两个操作数的类型可能不同。然而,如果更宽的话,左操作数的类型会更改为右边的操作数。

Example

+= 运算符是一个扩充运算符。它也被称为累加运算符,因为它在“a”中加上“b”,并将结果重新分配给变量。

以下是在 Python 中的增强赋值运算符:

  1. Augmented Addition Operator

  2. Augmented Subtraction Operator

  3. Augmented Multiplication Operator

  4. Augmented Division Operator

  5. Augmented Modulus Operator

  6. Augmented Exponent Operator

  7. Augmented Floor division Operator

Augmented Addition Operator (+=)

以下示例将有助于理解 "+=" 运算符如何工作:

a=10
b=5
print ("Augmented addition of int and int")
a+=b # equivalent to a=a+b
print ("a=",a, "type(a):", type(a))

a=10
b=5.5
print ("Augmented addition of int and float")
a+=b  # equivalent to a=a+b
print ("a=",a, "type(a):", type(a))

a=10.50
b=5+6j
print ("Augmented addition of float and complex")
a+=b #equivalent to a=a+b
print ("a=",a, "type(a):", type(a))

它将生成以下 output

Augmented addition of int and int
a= 15 type(a): <class 'int'>
Augmented addition of int and float
a= 15.5 type(a): <class 'float'>
Augmented addition of float and complex
a= (15.5+6j) type(a): <class 'complex'>

Augmented Subtraction Operator (-=)

使用 -= 符号在单个语句中执行减法和赋值操作。"a-=b" 语句执行 "a=a-b" 赋值。操作数可以是任意数字类型。Python 针对大小较窄的 object 执行隐式类型转换。

a=10
b=5
print ("Augmented subtraction of int and int")
a-=b #equivalent to a=a-b
print ("a=",a, "type(a):", type(a))

a=10
b=5.5
print ("Augmented subtraction of int and float")
a-=b #equivalent to a=a-b
print ("a=",a, "type(a):", type(a))

a=10.50
b=5+6j
print ("Augmented subtraction of float and complex")
a-=b #equivalent to a=a-b
print ("a=",a, "type(a):", type(a))

它将生成以下 output

Augmented subtraction of int and int
a= 5 type(a): <class 'int'>
Augmented subtraction of int and float
a= 4.5 type(a): <class 'float'>
Augmented subtraction of float and complex
a= (5.5-6j) type(a): <class 'complex'>

Augmented Multiplication Operator (*=)

“@ {s0}=b”执行乘法和赋值操作,相当于“a=a*b”。对于两个复数的扩充乘法,上一章讨论的乘法规则适用。

a=10
b=5
print ("Augmented multiplication of int and int")
a*=b #equivalent to a=a*b
print ("a=",a, "type(a):", type(a))

a=10
b=5.5
print ("Augmented multiplication of int and float")
a*=b #equivalent to a=a*b
print ("a=",a, "type(a):", type(a))

a=6+4j
b=3+2j
print ("Augmented multiplication of complex and complex")
a*=b #equivalent to a=a*b
print ("a=",a, "type(a):", type(a))

它将生成以下 output

Augmented multiplication of int and int
a= 50 type(a): <class 'int'>
Augmented multiplication of int and float
a= 55.0 type(a): <class 'float'>
Augmented multiplication of complex and complex
a= (10+24j) type(a): <class 'complex'>

Augmented Division Operator (/=)

组合符号“/=”用作除法和赋值运算符,因此“a/=b”相当于“a=a/b”。int 或 float 操作数的除法运算为 float。两个复数的除法返回一个复数下文给出扩充除法运算符的示例。

a=10
b=5
print ("Augmented division of int and int")
a/=b #equivalent to a=a/b
print ("a=",a, "type(a):", type(a))

a=10
b=5.5
print ("Augmented division of int and float")
a/=b #equivalent to a=a/b
print ("a=",a, "type(a):", type(a))

a=6+4j
b=3+2j
print ("Augmented division of complex and complex")
a/=b #equivalent to a=a/b
print ("a=",a, "type(a):", type(a))

它将生成以下 output

Augmented division of int and int
a= 2.0 type(a): <class 'float'>
Augmented division of int and float
a= 1.8181818181818181 type(a): <class 'float'>
Augmented division of complex and complex
a= (2+0j) type(a): <class 'complex'>

Augmented Modulus Operator (%=)

如需在单个语句中执行取模和赋值运算,请使用 %= 运算符。与 mod 运算符一样,其增强的版本也不支持复数。

a=10
b=5
print ("Augmented modulus operator with int and int")
a%=b #equivalent to a=a%b
print ("a=",a, "type(a):", type(a))

a=10
b=5.5
print ("Augmented modulus operator with int and float")
a%=b #equivalent to a=a%b
print ("a=",a, "type(a):", type(a))

它将生成以下 output

Augmented modulus operator with int and int
a= 0 type(a): <class 'int'>
Augmented modulus operator with int and float
a= 4.5 type(a): <class 'float'>

Augmented Exponent Operator (**=)

“**=”运算符会导致计算“a”的“b”次方,并将值赋回给“a”。下面给出了几个示例:

a=10
b=5
print ("Augmented exponent operator with int and int")
a**=b #equivalent to a=a**b
print ("a=",a, "type(a):", type(a))

a=10
b=5.5
print ("Augmented exponent operator with int and float")
a**=b #equivalent to a=a**b
print ("a=",a, "type(a):", type(a))

a=6+4j
b=3+2j
print ("Augmented exponent operator with complex and complex")
a**=b #equivalent to a=a**b
print ("a=",a, "type(a):", type(a))

它将生成以下 output

Augmented exponent operator with int and int
a= 100000 type(a): <class 'int'>
Augmented exponent operator with int and float
a= 316227.7660168379 type(a): <class 'float'>
Augmented exponent operator with complex and complex
a= (97.52306038414744-62.22529992036203j) type(a): <class 'complex'>

Augmented Floor division Operator (//=)

如需在单个语句中执行取整除和赋值,请使用 “//=”运算符。“a//=b”相当于“a=a//b”。此运算符不能用于复数。

a=10
b=5
print ("Augmented floor division operator with int and int")
a//=b #equivalent to a=a//b
print ("a=",a, "type(a):", type(a))

a=10
b=5.5
print ("Augmented floor division operator with int and float")
a//=b #equivalent to a=a//b
print ("a=",a, "type(a):", type(a))

它将生成以下 output

Augmented floor division operator with int and int
a= 2 type(a): <class 'int'>
Augmented floor division operator with int and float
a= 1.0 type(a): <class 'float'>