在本節中,我們將討論 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
按位 And (&)和賦值運算符(&=)
按位 And (&)和賦值運算符用於對(左和右)操作數進行運算,並將結果賦給左操作數。
語法
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
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/130351.html