詹惠儿

2018-12-07   阅读量: 595

数据分析师 Python数据分析 Python编程

python的可变目标

扫码加入数据分析学习群

可变目标

Inplace运算符在可变目标(如列表和字典)中的行为与普通运算符不同。更新和赋值都是在可变目标的情况下执行的。

# Python code to demonstrate difference between

# Inplace and Normal operators in mutable Targets

# importing operator to handle operator operations

import operator

# Initializing list

a = [1, 2, 4, 5]

# using add() to add the arguments passed

z = operator.add(a,[1, 2, 3])

# printing the modified value

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

print (z)

# printing value of first argument

# value is unchanged

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

print (a)

# using iadd() to add the arguments passed

# performs a+=[1, 2, 3]

p = operator.iadd(a,[1, 2, 3])

# printing the modified value

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

print (p)

# printing value of first argument

# value is changed

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

print (a)

添加CDA认证专家【维克多阿涛】,微信号:【cdashijiazhuang】,提供数据分析指导及CDA考试秘籍。已助千人通过CDA数字化人才认证。欢迎交流,共同成长!
0.0000 0 1 关注作者 收藏

评论(0)


暂无数据

推荐课程

推荐帖子