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 中不同類型的賦值運算符:

  1. 簡單賦值運算符(=)
  2. 加法和相等運算符(+=)
  3. 加減運算符(-=)
  4. 星號和等號運算符(*=)
  5. 除與等運算符(/=)
  6. 模數和相等運算符(%=)
  7. 雙除等運算符(//=)
  8. 指數賦值運算符(**=)
  9. 按位「與」運算符(&=)
  10. 按位或運算符(|=)
  11. 按位異或賦值運算符(^=)
  12. 按位右移賦值運算符(> > =)
  13. 按位左移賦值運算符(< < =)

賦值運算符(=)

簡單賦值運算符將右側操作數表達式或值賦給左側操作數。

語法


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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
簡單一點的頭像簡單一點
上一篇 2024-10-03 23:28
下一篇 2024-10-03 23:28

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • 蝴蝶優化演算法Python版

    蝴蝶優化演算法是一種基於仿生學的優化演算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化演算法Python版…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29

發表回復

登錄後才能評論