传递列表
def addlist(origin,x):
origin.append(x)
x+=1
origin=["ggg"]
x=3
addlist(origin[:],x)#切片创建列表副本
print(origin)
addlist(origin,x)
addlist(origin,x)
print(origin)
参数为切片则函数所做的修改不会影响原列表,以上运行结果为:
['ggg']
['ggg', 3, 3]
传递任意数量的实参:
def make_cake(size,*topping):
print(type(topping))
print("make a "+str(size)+" cake with",end=' ')
for t in topping:
print(t,end=' ')
运行结果为:
<class 'tuple'>
make a 16 cake with cream chocolate
def make_likes(**info):
print(type(info))
#print("make a "+str(size)+" cake with",end=' ')
for k,v in info.items():
print("{0} likes {1}".format(k, v))
make_likes(tom='banana',jerry='apple')
运行结果为:
<class 'dict'>
tom likes banana
jerry likes apple