日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python運算符之Inplace運算符的使用教程_python

作者:海擁 ? 更新時間: 2022-11-07 編程語言

Python 在其定義中提供了執行就地操作的方法,即使用“?operator?”模塊在單個語句中進行賦值和計算。 例如,

x += y is equivalent to x = operator.iadd(x, y) 

一些重要的就地操作

1. iadd() ?:- 該函數用于分配和添加當前值。該操作執行“?a+=b?”操作。在不可變容器(例如字符串、數字和元組)的情況下執行分配。

2. iconcat() ?:- 該函數用于在第二個末尾連接一個字符串

# 演示 iadd() 和 iconcat() 工作的 Python 代碼

# importing operator to handle operator operations
import operator

# 使用 iadd() 添加和賦值
x = operator.iadd(2, 3);

# 打印修改后的值
print ("添加賦值后的值為:", end="")
print (x)

# 初始化值
y = "geeks"

z = "forgeeks"

# 使用 iconcat() 連接序列
y = operator.iconcat(y, z)

# 使用 iconcat() 連接序列
print ("拼接后的字符串為:", end="")
print (y)

輸出:

添加賦值后的值為:5
拼接后的字符串為:geeksforgeeks

3. isub() ?:- 該函數用于分配和減去當前值。該操作執行“?a-=b?”操作。在不可變容器(例如字符串、數字和元組)的情況下執行分配。

4. imul() ?:- 該函數用于分配和乘以當前值。該操作執行“?a=b*?”操作。在不可變容器(例如字符串、數字和元組)的情況下執行分配。

# 演示 isub() 和 imul() 工作的 Python 代碼

# importing operator to handle operator operations
import operator

# 使用 isub() 減去和賦值
x = operator.isub(2, 3);

# 打印修改后的值
print ("減法運算后的值:", end="")
print (x)

# 使用 imul() 進行乘法和賦值
x = operator.imul(2, 3);

# 打印修改后的值
print ("乘法運算后的值:", end="")
print (x)

輸出:

減法運算后的值:-1?
乘法運算后的值:6

5. itruediv() ?:- 該函數用于對當前值進行賦值和除法。此操作執行“?a/=b?”操作。在不可變容器(例如字符串、數字和元組)的情況下執行分配。

6. imod() ?:- 該函數用于分配和返回余數。該操作執行“?a%=b?”操作。在不可變容器(例如字符串、數字和元組)的情況下執行分配。

# 演示 itruediv() 和 imod() 工作的 Python 代碼

# importing operator to handle operator operations
import operator

# 使用 itruediv() 進行除法賦值
x = operator.itruediv(10, 5);

# 打印修改后的值
print ("除法賦值后的值:", end="")
print (x)

# 使用 imod() 取模并賦值
x = operator.imod(10, 6);

# 打印修改后的值
print ("取模賦值后的值:", end="")
print (x)

輸出:

除法賦值后的值:2.0
取模賦值后的值:4

原文鏈接:https://juejin.cn/post/7143436032090996749

欄目分類
最近更新