热线电话:13121318867

登录
2019-03-30 阅读量: 440
什么是不可变的目标

不可变的目标

在不可变目标中,例如数字,字符串和元组。Inplace运算符的行为与普通运算符相同,即只进行赋值,在传递的参数中不进行任何修改。

# Python code to demonstrate difference between

# Inplace and Normal operators in Immutable Targets

# importing operator to handle operator operations

import operator

# Initializing values

x = 5

y = 6

a = 5

b = 6

# using add() to add the arguments passed

z = operator.add(a,b)

# using iadd() to add the arguments passed

p = operator.iadd(x,y)

# printing the modified value

print ("Value after adding using normal operator : ",end="")

print (z)

# printing the modified value

print ("Value after adding using Inplace operator : ",end="")

print (p)

# printing value of first argument

# value is unchanged

print ("Value of first argument using normal operator : ",end="")

print (a)

# printing value of first argument

# value is unchanged

print ("Value of first argument using Inplace operator : ",end="")

print (x)

输出:

Value after adding using normal operator : 11
Value after adding using Inplace operator : 11
Value of first argument using normal operator : 5
Value of first argument using Inplace operator : 5
0.0000
0
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子