defmake_shirt(size,word):print('the size of T-shirt is '+str(size).upper()+',the word demo is '+word)#位置实参
make_shirt('xxl','peace and love')# the size of T-shirt is XXL,the word demo is peace and love#关键字实参
make_shirt(word='peace and love',size ='xl')# the size of T-shirt is XL,the word demo is peace and love
8.2.4 动手做-大号T恤
defmake_shirt(size='xxl',word='I love python'):print('the size of T-shirt is '+str(size).upper()+',the word demo is '+word.title())#默认
make_shirt()# the size of T-shirt is XXL,the word demo is I Love Python#默认字样,中号
make_shirt('xl')# the size of T-shirt is XL,the word demo is I Love Python#非默认
make_shirt('xl','peace and love')# the size of T-shirt is XL,the word demo is Peace And Love
person ={}defredict(name,gender):
person[name]= gender#对字典的操作是永久的
redict('libinglin','male')
redict('zhoujielun','male')
redict('shiwaxingge','male')for name,gender in person.items():print(name+" is "+gender)# libinglin is male# zhoujielun is male# shiwaxingge is male
8.3.4 构建专辑字典函数
defmake_album(name,album,count=1):
albums ={}
albums['name']= name
albums[album]= album
albums['count']= count
return albums
album1 = make_album('泰勒斯威夫特','1989',11)print(album1)
album2 = make_album('林俊杰','江南',2)print(album2)
album3 = make_album('周杰伦','mojito')print(album3)# {'name': '泰勒斯威夫特', '1989': '1989', 'count': 11}# {'name': '林俊杰', '江南': '江南', 'count': 2}# {'name': '周杰伦', 'mojito': 'mojito', 'count': 1}
8.4.1 音乐人
songers =['周杰伦','林俊杰','郭富城','刘德华']defprintSonger(s):for a in s:print(a)
printSonger(songers)# 周杰伦# 林俊杰# 郭富城# 刘德华defmake_great(s):for i inrange(0,len(s)):
s[i]='The great '+ s[i]# 传入副本
make_great(songers[:])
printSonger(songers)# 周杰伦# 林俊杰# 郭富城# 刘德华#传入本身
make_great(songers)
printSonger(songers)# The great 周杰伦# The great 林俊杰# The great 郭富城# The great 刘德华
8.5.1 传递任意数量的实参
#*con代表传入的是元组,用圆括号定义,访参考列表defmake_pizza(*con):# 遍历参数for a in con:print(a)
make_pizza('apple')# apple
8.5.2结合位置实参和任意实参
defget_name(first_name,*last_name):
full_name = first_name
for name in last_name:
full_name = full_name + name
print(full_name)
get_name('周','杰','伦')# 周杰伦
8.5.3 使用任意数量的关键字实参
# **user_info是字典defbuild_profile(first,last,**user_info):
profile ={}
profile['first_name']= first
profile['last_name']= last
for key,value in user_info.items():
profile[key]= value
return profile
profile_zhou = build_profile('周','杰伦',weight='80kg',
height=180,
address='taiwan')for key,value in profile_zhou.items():print(key+":"+str(value))# first_name:周# last_name:杰伦# weight:80kg# height:180# address:taiwan