''''''
################################################################################################################
'''定义函数'''
'''
def greet():
print('hello')
#调用函数
greet()
'''
'''函数接受指定值,实参->形参'''
'''
def greet(user): #user是形参
print('hello,'+user)
#调用时,给函数传一个值
greet('GodF') #GodF是实参
#hello,GodF
'''
'''位置实参 实参和形参的位置顺序相同'''
'''
def fun(p1,p2):
print('you like '+p1)
print('you dont like '+p2)
fun('bmw','byd') #即’bmw‘对应p1,’byd‘对应p2
#you like bmw
#you dont like byd
'''
'''关键字实参 形参=实参 顺序不重要'''
'''
def fun(p1,p2):
print('you like '+p1)
print('you dont like '+p2)
fun(p1='bmw',p2='byd')
#you like bmw
#you dont like byd
'''
'''形参默认值,先列出没有默认值的形参p1,再列出有默认值的p2.'''
'''
def fun(p1,p2='audi'):
print('you like '+p1)
print('you dont like '+p2)
fun(p1='bmw')#调用时未指定p2
#you like bmw
#you dont like audi
fun(p1='bmw',p2='byd')#调用指定p2
#you like bmw
#you dont like byd
'''
'''返回值,return语句将值返回到'调用函数'的代码行'''
'''
def new_name(part1,part2):
full_name=part1+' '+part2
return full_name.title()
name=new_name('gang','dong')
print(name)
#Gang Dong
'''
'''可选实参,在该实参对应的形参列表最后,设置一个默认值,形参=‘ ’ '''
'''
def make_name(first,last,mid=''):
if mid: #如果形参mid(存在)不为空
new_name = first + ' ' + mid + ' ' + last
else:
new_name = first + ' ' + last
return new_name.title()
name1=make_name('fu','gang')
print(name1)
name2=make_name('li','ru','meng')
print(name2)
#Fu Gang
#Li Meng Ru
'''
'''返回字典'''
'''
def person(first_person,second_person):
name={'first':first_person,'second':second_person}#把形参存储到字典
return name #返回字典
message=person('gang','dong')
print(message)
#{'first': 'gang', 'second': 'dong'}
'''
'''设置一个可选实参age'''
'''
def person(first_person,second_person,age=''):
name={'first':first_person,'second':second_person}#把形参存储到字典
if age:
name['age']=age #添加age到字典
return name #返回字典
message=person('gang','dong')#实参没有age
print(message)
#{'first': 'gang', 'second': 'dong'}
message=person('gang','dong',27)#实参有age
print(message)
#{'first': 'gang', 'second': 'dong', 'age': 27}
print(type(message['age']))
#<class 'int'>
'''
'''结合使用函数+while循环'''
'''
def new_name(part1,part2):
full_name=part1+' '+part2
return full_name.title()
while True:
print('please input your name')
print("enter 'quit' to quit")
p1_name=input('enter p1 name:')
if p1_name=='quit':
break
p2_name=input('enter p2 name:')
if p2_name=='quit':
break
message=new_name(p1_name,p2_name)
print(message)
#Gang Dong
'''
'''传递列表'''
'''
def greet_user(names):
for name in names:
msg='hello, '+name.title()+'!'
print(msg)
usernames=['gang','dong','laing']
greet_user(usernames)
#hello, Gang!
#hello, Dong!
#hello, Laing!
'''
'''在函数中修改列表'''
'''
def print_models(designs,models):
while designs:
design=designs.pop()
print('print model:'+design)
models.append(design)
def show_models(models):
print('the follows model have been printed')
for model in models:
print(model)
designs=['gang','liang','dong']
models=[]
print_models(designs,models)
show_models(models)
'''
'''禁止函数修改列表,使用[:]创建列表的副本'''
'''
def print_models(designs,models):
while designs:
design=designs.pop()
print('print model:'+design)
models.append(design)
designs=['gang','liang','dong']
models=[]
print_models(designs,models)
print(designs)
#[]
#使用[:]创建列表的副本
print_models(designs[:],models)
print(designs)
#['gang', 'liang', 'dong']
'''
'''传递任意数量的实参*toppings'''
'''
def make_pizza(*toppings):#*topping创建一个元组
print(toppings)
make_pizza('naiyou')
#('naiyou',)
make_pizza('aaa','sss','dddd')
'''
'''#替换为一个循环
def make_pizza(*toppings):
for topping in toppings:
print('-'+topping)
make_pizza('naiyou')
make_pizza('aaa','sss','dddd')
'''结合使用位置实参和任意数量实参'''
'''
def make_pizza(size,*toppings):#*topping放在最后
print('\nmaking a '+str(size)+'-inch pizza with the following topping:')
for topping in toppings:
print('-' + topping)
make_pizza(16,'naiyou')
make_pizza(12,'aaa','sss','dddd')
'''
'''使用任意数量的关键字实参'''
'''
def build_profile(first,last,**user_info):#**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('aaa','bbb',location='ccc',filed='ddd')
print(user_profile)
#{'first_name': 'aaa', 'last_name': 'bbb', 'location': 'ccc', 'filed': 'ddd'}
'''
'''将函数存储在模块中,模块是.py文件'''
'''
#导入整个模块
import pizza
#调用模块内函数
pizza.make_pizza(16,'naiyou')
pizza.make_pizza(12,'aaa','sss','dddd')
'''
'''导入特定的函数,若需要导入同一模块内的多个函数,用逗号,分割'''
'''
#从模块中导入函数
from pizza import make_pizza
make_pizza(16,'naiyou')
make_pizza(12,'aaa','sss','dddd')
'''
'''使用as给函数指定别名'''
'''
#给make_pizza函数指定别名mp
from pizza import make_pizza as mp
mp(16,'naiyou')
mp(12,'aaa','sss','dddd')
'''
'''使用as给模块指定别名'''
'''
#给pizza模块指定别名p
import pizza as p
p.make_pizza(16,'naiyou')
p.make_pizza(12,'aaa','sss','dddd')
'''
'''导入模块中的所有函数,使用*运算符'''
'''
#导入pizza模块中的所有函数
from pizza import *
make_pizza(16,'naiyou')
make_pizza(12,'aaa','sss','dddd')
'''
python教程十一(函数)
最新推荐文章于 2023-12-28 09:58:45 发布