defdisplay_message():
print("I will learn the usage of function in this chapter!")
display_message()
In [1]: I will learn the usage offunctioninthischapter!
8-3
defmake_shirt(size, font):
print("This T-shirt is " + str(size) + " big, the '" + font + "' is on it")
make_shirt(32,"hello")
make_shirt(size = 32, font = "hello")
In [2]: runfile(...)
This T-shirt is 32 big, the 'hello' is on it
This T-shirt is 32 big, the 'hello' is on it
8-6
defcity_country(city,country):return str(city.title() + ', ' + country.title())
cc = [('beijing','China'),('New York','American'),('St.Petersburg','Russia')]
for city,country in cc:
print(city_country(city,country))
In [3]: runfile(...)
Beijing, China
New York, American
St.Petersburg, Russia
8-9
defshow_magicians(megicians):for me in megicians:
print(me)
megicians = ['wuda',"wuer","wusan"]
show_magicians(megicians)
In [4]: runfile(...)
wuda
wuer
wusan
8-13
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
user_profile = build_profile('huang', 'dirun',
age=20,
field='cs',
favorite='sports'
)
print(user_profile)