Python 賦值運算子



Python 賦值運算子

在 Python 中,`=`(等於)符號被定義為賦值運算子。右側 Python 表示式的值被賦值給左側的單個變數。程式設計中(尤其是在 Python 中)的`=`符號不應與數學中的用法混淆,在數學中它表示符號兩側的表示式相等。

Python 中賦值運算子示例

考慮以下 Python 語句:

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

對於程式設計新手來說,但對於瞭解數學的人來說,“a=a+b”這個語句看起來很奇怪。a 怎麼可能等於“a+b”?但是,需要再次強調的是,這裡的`=`符號是賦值運算子,而不是用來表示左側和右側相等的。

因為它是一個賦值操作,右側表示式計算結果為 15,該值被賦值給 a。

在語句“a+=b”中,兩個運算子“+”和“=”可以組合成一個“+=”運算子。它被稱為加法賦值運算子。它在一個語句中執行兩個運算元“a”和“b”的加法,並將結果賦值給左側的運算元“a”。

Python 中的增強賦值運算子

除了簡單的賦值運算子外,Python 還提供了一些增強賦值運算子用於高階用法。它們被稱為累積或增強賦值運算子。在本節中,我們將學習如何在 Python 中使用定義的增強賦值運算子。

Python 為所有算術比較運算子提供了增強賦值運算子。

Python 增強賦值運算子將加法和賦值組合在一個語句中。由於 Python 支援混合算術運算,因此兩個運算元可以是不同型別。但是,如果右運算元型別更寬,則左運算元的型別會更改為右運算元的型別。

示例

+= 運算子是一個增強運算子。它也稱為累加運算子,因為它將“b”新增到“a”中,並將結果賦值回變數。

以下是 Python 中的增強賦值運算子

  • 增強加法運算子
  • 增強減法運算子
  • 增強乘法運算子
  • 增強除法運算子
  • 增強取模運算子
  • 增強指數運算子
  • 增強地板除運算子

增強加法運算子 (+=)

以下示例將幫助您理解 "+=" 運算子的工作方式:

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))

它將產生以下輸出

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'>

增強減法運算子 (-=)

使用 -= 符號在一個語句中執行減法和賦值操作。“a -= b”語句執行“a = a - b”賦值。運算元可以是任何數字型別。Python 對大小較小的物件執行隱式型別轉換。

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))

它將產生以下輸出

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'>

增強乘法運算子 (*=)

*= 運算子的工作原理類似。“a *= 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))

它將產生以下輸出

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'>

增強除法運算子 (/=)

組合符號 "/=" 充當除法和賦值運算子,因此“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))

它將產生以下輸出

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'>

增強取模運算子 (%=)

要在單個語句中執行取模和賦值操作,請使用 %= 運算子。與 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))

它將產生以下輸出

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'>

增強指數運算子 (**=)

**= 運算子計算“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))

它將產生以下輸出

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'>

增強地板除運算子 (//=)

要在單個語句中執行地板除和賦值,請使用 //= 運算子。“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))

它將產生以下輸出

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'>
廣告