Python 中的原地運算子 - ixor()、iand()、ipow()


本文中,我們將學習 Python 3.x 及更早版本中的一些原地運算子。

Python 提供方法來執行原地操作,即在單個語句中使用“運算子”模組同時完成賦值和計算。本文中,我們將討論 ixor()、iand() 和 ipow() 函式。

ixor()

該函式允許我們賦值並異或當前值。此操作的行為類似於“a^=b”操作。對於不可變資料型別(如字串和元組)無法執行賦值。

示例

import operator as op

# using ixor() to xor
int1 = op.ixor(786,12);

# displaying value
print ("The value : ", end="")
print (int1)

輸出

The value : 798

iand()

該函式允許我們賦值並按位與當前值。此操作的行為類似於“a&=b”操作。對於不可變資料型別(如字串和元組)無法執行賦值。

示例

# using iand() to bitwise&
int2 = op.iand(57,34)

print ("The value : ", end="")
print (int2)

輸出

The value : 32

ipow()

該函式允許我們賦值並對當前值求冪。此操作的行為類似於“a**=b”操作。對於不可變資料型別(如字串和元組)無法執行賦值。

示例

# using ipow() to exponentiate
int2 = op.ipow(3,2)

print ("The value : ", end="")
print (int2)

輸出

The value : 9

結論

本文中,我們學習了 Python 中的原地運算子 ixor()、iand() 和 ipow() 的使用和實現。

更新於: 2019-08-07

184 人檢視

開啟你的 職業

完成課程以獲得認證

開始吧
廣告
© . All rights reserved.