# define function defgreet_user(): '''say hello''' print("Hello!") # use function greet_user() # output: Hello!
2.向函数传递信息
位置实参:
1 2 3 4 5 6 7 8
# define function defgreet_user(username): '''say hello''' print("Hello, " + username.title() + "!") # use function greet_user('hesse') # output: Hello, Hesse!
关键字实参:
1 2 3 4 5 6 7 8
# define function defgreet_user(username): '''say hello''' print("Hello, " + username.title() + "!") # use function greet_user(username='hesse') # output: Hello, Hesse!
参数默认值:(若无默认值,则必须进行参数传递)
1 2 3 4 5 6 7 8 9 10
# define function defgreet_user(username='tom'): '''say hello''' print("Hello, " + username.title() + "!") # use function greet_user(username='hesse') greet_user() # output: Hello, Hesse! Hello, Tom!
3.函数返回值
3.1返回简单值
1 2 3 4 5 6 7 8 9 10
# define function defget_full_name(first_name, last_name): '''return full name''' full_name = first_name + ' ' + last_name return full_name.title() # use function musician = get_full_name('jimi', 'hendrix') print(musician) # output: Jimi Hendrix
3.2返回字典
1 2 3 4 5 6 7 8 9 10
# define function defbuild_person(first_name, last_name): '''return a person dictionary''' person = {'first': first_name, 'last': last_name} return person # use function musician = build_person('jimi', 'hendrix') print(musician) # output: {'first': 'jimi', 'last': 'hendrix'}
4.传递列表
你经常会发现,向函数传递列表很有用。
4.1简单使用
将列表传递给函数后,函数就能直接访问其内容。假设有一个用户列表,我们要问候其中的每位用户。
1 2 3 4 5 6 7 8 9 10 11 12 13
# define function defgreer_users(names): '''greet to everyone in names''' for name in names: msg = "Hello, " + name.title() + "!" print(msg) # use function usernames = ['hannah', 'ty', 'margot'] greer_users(usernames) # output: Hello, Hannah! Hello, Ty! Hello, Margot!
# change list in function 在函数中修改列表 defprint_models(un_designs, com_models): ''' print every design in un_designs then put it in com_models 打印所有未打印列表的设计,并将他们放到已完成列表 ''' while unprinted_designs: current_design = un_designs.pop() print("Printing model: " + current_design) com_models.append(current_design) defshow_completed_models(com_models): '''show completed_models 显示已完成列表''' print("\nThe following models have been printed:") for completed_model in completed_models: print(completed_model) # define list unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron'] completed_models = [] # use function print_models(unprinted_designs, completed_models) show_completed_models(completed_models) # output: Printing model: dodecahedron Printing model: robot pendant Printing model: iphone case
The following models have been printed: dodecahedron robot pendant iphone case
# define function 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 # use function user_profile = build_profile('albert', 'einstein', location='princeton', field='physics') print(user_profile) # output: {'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton', 'field': 'physics'}
from pprint import pprint sentence = "This is a common interview question" char_frequency = {} for char in sentence: if char in char_frequency: char_frequency[char] += 1 else: char_frequency[char] = 1 pprint(char_frequency, width=1)