1
#
#---------------------- 函数 ----------------------
2
3
#
#1 定义函数
4
def
hello_py():
5
print(‘hello,python!‘)
6 7hello_py()
8##1.1向函数传递消息 9def get_message(username):
10print(‘Hello,‘+username.title()+‘!‘)
11 name=input(‘请输入你的姓名:‘)
12get_message(name)
13##1.2实参和形参 14‘‘‘ 15前面定义函数 get_message() 时,要求给变量 username 指定一个值。调用这个函数并提供这种
16信息(人名)时,它将打印相应的问候语。
17在函数 get_message() 的定义中,变量 username 是一个形参——函数完成其工作所需的一项信
18息。在代码 get_message(name) 中,值 name 是一个实参。实参是调用函数时传递给函数的信
19息。我们调用函数时,将要让函数使用的信息放在括号内。在 get_message(name) 中,将实参
20name的值 传递给了函数 get_message() ,这个值被存储在形参 username 中。
21 22形参和实参位置顺序要一一对应
23函数也可以多次调用
24基本上与C语言一样
25‘‘‘ 26##1.3默认值 27‘‘‘ 28编写函数时,可给每个形参指定默认值。在调用函数中给形参提供了实参时,Python将使用
29指定的实参值;否则,将使用形参的默认值。因此,给形参指定默认值后,可在函数调用中省略
30相应的实参。使用默认值可简化函数调用,还可清楚地指出函数的典型用法。
31‘‘‘ 32def describe_pet(pet_name,animal_type=‘哈士奇‘):
33#显示宠物的信息 34print(‘我在家养过宠物,我给它起名叫做:‘+pet_name)
35print(pet_name+‘是一只非常好的‘+animal_type)
36 describe_pet(‘神奇‘)##第二个参数使用默认值 37 38###1.4各种调用方法 39 describe_pet(‘传奇‘)
40 describe_pet(pet_name=‘传奇‘)
41 describe_pet(‘可可‘,‘猫‘)
42 name=‘巧巧‘ 43 an_type=‘猫‘ 44 describe_pet(name,an_type)##利用别的变量传递 45##1.5返回简单值 46def get_formatted_name(first_name,last_name):
47 full_name=last_name+‘‘+first_name
48return full_name.title()
49 musician=get_formatted_name(‘复‘,‘慕容‘)
50print(musician)
51##1.5让实参变成可选的 52‘‘‘ 53有时候,需要让实参变成可选的,这样使用函数的人就只需在必要时才提供额外的信息。可
54使用默认值为空来让实参变成可选的。
55‘‘‘ 56##返回字典 57def build_person(first_name,last_name,age=‘‘):##此处利用age默认值为空实现了参数的可选择性 58##返回一个在字典,其中包括一个人信息 59 person={‘first‘:first_name,‘last‘:last_name}
60if age:
61 person[‘age‘]=age
62return person
63 musician=build_person(‘锋‘,‘欧阳‘)
64print(musician)
65print(‘---------------------------------‘)
66##函数和while循环的结合 67 circulation=True##循环变量 68while circulation:
69print(‘输入你的名字和姓氏:‘)
70 f_name=input(‘first_name:‘)
71 l_name=input(‘last_name:‘)
72 full_name=build_person(f_name,l_name)
73print(‘你好‘+str(full_name[‘first‘]+str(full_name[‘last‘])))
74print(‘后面,还有小朋友吗?(yes/no)‘)
75 answer=input()
76if answer==‘no‘or answer==‘NO‘or answer==‘n‘:
77 circulation=False
78 79##传递列表 80print(‘-----------------------------------------‘)
81def greet_user(names):
82for name in names:
83 msg=‘hello‘+name.title()+‘!‘ 84print(msg)
85 86 user_list=[‘小明‘,‘小华‘,‘杰克‘]
87greet_user(user_list)
88##在函数中修改列表 89print(‘---------------------------------------------‘)
90 91 92##函数后序内容 93‘‘‘ 94将列表传递给函数后,函数就可对其进行修改。在函数中对这个列表所做的任何修改都是永
95久性的,这让你能够高效地处理大量的数据。
96来看一家为用户提交的设计制作3D打印模型的公司。需要打印的设计存储在一个列表中,
97打印后移到另一个列表中。
98‘‘‘ 99def print_models(unprinted_designs,completed_models):
100##打印每个设计后,都将其移到列表completed_models中101while unprinted_designs:
102 current_design=unprinted_designs.pop()
103104##模拟根据要求的打印过程105print(‘正在打印中的模型是:‘+current_design)
106 completed_models.append(current_design)
107def show_completed_models(completed_models):
108##显示所有打印好的模型109print(‘打印完成的模型列表:n‘)
110for print_over_models in completed_models:
111print(‘t‘+print_over_models)
112113 unprinted_designs=[‘160_轰炸机‘,‘米格——3k拦截机‘,‘苏——57战斗机‘,‘F22战斗机‘]
114 completed_models=[]
115print_models(unprinted_designs,completed_models)
116print(‘nn‘)
117show_completed_models(completed_models)
118119##禁止函数修改列表(利用列表切片实现)120##例:print_models(unprinted_designs[:],completed_models[:])121122##传递任意数量的参数123def make_pizza(*topping):
124##其中实参*topping是一个元组,所以可以传递任意数量的参数125‘‘‘打印顾客点的所有配料‘‘‘126print(topping)
127128 make_pizza(‘洋葱‘)
129 make_pizza(‘洋葱‘,‘鸡蛋‘,‘面粉‘,‘培根‘)
130131##使用任意数量的关键字实参132##其中,下面的**user_info可以理解为一个字典133def build_profile(first,last,**user_info):
134‘‘‘创建一个字典,其中包含我们知道的有关用户的一切‘‘‘135 profile={}
136 profile[‘first_name‘]=first
137 profile[‘last_name‘]=last
138for key,value in user_info.items():
139 profile[key]=value
140return profile
141142 user_profile=build_profile(‘赵‘,‘飞燕‘,性别=‘女‘,职业=‘舞蹈艺术家‘)
143print(user_profile)
原文:https://www.cnblogs.com/best-mingge/p/12866975.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!