Python中的Inplace运算符-iadd(),isub(),iconcat()
在本文中,我们将学习Python3.x中可用的一些inplace运算符。或更早。
Python提供了一些方法来执行就地操作,即借助“operator”模块在单个语句中同时执行赋值和计算。在这里,我们将讨论iadd(),isub()和iconcat()函数。
我加()
此功能使我们可以分配和添加当前值。此操作的行为类似于“a+=b”操作。如果数据类型是不变的,例如字符串和元组,则不能执行分配。
示例
import operator as op # using iadd() to add int1 = op.iadd(786,0); # displaying value print ("The value : ", end="") print (int1)
输出结果
The value : 786
isub()
此功能使我们可以分配和减去当前值。此操作的行为类似于“a-=b”操作。如果数据类型是不变的,例如字符串和元组,则不能执行分配。
示例
# using isub() to subtract int2 = op.isub(57,34) print ("The value : ", end="") print (int2)
输出结果
: The value : 23
iconcat()
此函数使我们可以在第二个字符串的末尾连接一个字符串,就像对字符串的加法运算符一样。
示例
str1 = "tutorials" str2 = "point" # using iconcat() to concatenation str1 = op.iconcat(str1, str2) # displaying value print ("The string becomes: ", end="") print (str1)
输出结果
The string becomes: nhooo
结论
在本文中,我们了解了Python中Inplace运算符的用法和实现-iadd(),isub(),iconcat()。