1.定义函数
#下面定义一个简单的函数, def关键字用来定义函数
def greet_user(username):
print(f"hello-{username}")
#调用
greet_user('jesse')
#实参和形参,上面变量username是一个形参(parameter),greet_user('jesse') 的jesse是一个实参
2.关键字实参
def greet_user(username,age):
print(f"hello-{username}")
print(f"age-{age}")
#调用时不会混淆
greet_user(username='jesse',age=10)
3.默认值
#形参指定默认值
def greet_user(username="张三",age=22):
print(f"hello-{username}")
print(f"age-{age}")
#调用时不会混淆
greet_user()
greet_user(username="李四")
greet_user(age=30)
4. 演示返回简单值
#演示返回简单值
def get_name(username,last_name):
fullname=f"{username}{last_name}"
return fullname.title()
#调用函数获取返回值
musician=get_name(username="jimi",last_name="hendrix")
print(musician)
5.演示返回字典
#演示返回字典
def get_name(first_name,last_name,age=None):
person={"first":first_name,"last":last_name}
if age:
person["age"]=age
return person
#调用函数获取返回值
musician=get_name(first_name="jimi",last_name="hendrix",age=30)
print(musician)
6.使用列表作为形参
#演示使用列表做为形参
def greet_users(names):
for name in names:
msg=f'hello,{name.title()}'
print(msg)
#调用函数
names=['hannah','ty','margot']
greet_users(names)
7.在函数中修改列表
#在函数中修改列表
def print_models(unprinted_designs,completed_models):
while unprinted_designs:
current=unprinted_designs.pop()
print(f"printing model:{current}")
completed_models.append(current)
def show_completed_models(completed_models):
for completed in completed_models:
print(completed)
unprinted_designs = ['phone case', 'robot pendant', 'dodecahedron']
completed_models=[]
print_models(unprinted_designs,completed_models)
show_completed_models(completed_models)
8传递任意数量的实参
#形参以*开头的,支持任意数量的实参
def make_pizza(*toppings):
print(toppings)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
9.结合实参和支持任意数量的实参
#结合实参和支持任意数量的实参
def make_pizza(size,*toppings):
print(f"\nMaking a {size}-inch pizza with the following toppings:")
for top in toppings:
print(f"-{top}")
make_pizza(16,'pepperoni')
make_pizza(12,'mushrooms', 'green peppers', 'extra cheese')
10. 使用任意数量的关键字实参
# 形参**user_info中的两个星号让python创建一个名为user_info的空字典,
#并将收到的所有名称值对都放在这个字典中。
def build_profile(first, last, **user_info):
#创建一个字典,其中包含我们知道的有关用户的一切
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
user_profile = build_profile('albert', 'einstein',
location='princeton',
field='physics',
field2='physics2')
print(user_profile)
11.导入整个模块
使用模块,将函数存储在独立文件中,再将模块导入到主程序中。这样相当于C# 类调用另一个类中的方法
创建一个模块,文件名为function_sample_model.py,内容如下所示:
# 形参**user_info中的两个星号让python创建一个名为user_info的空字典,
#并将收到的所有名称值对都放在这个字典中。
def build_profile(first, last, **user_info):
#创建一个字典,其中包含我们知道的有关用户的一切
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
创建一个名为function_sample.py的文件, 二个文件在同一目录
#使用import导入整个模块,模块文件为function_sample_model.py
import function_sample_model
user_profile=function_sample_model.build_profile('albert', 'einstein',
location='princeton',
field='physics',
field2='physics2')
print(user_profile)
12.导入模块中指定的函数
#导入指定的函数,这里导入了build_profile和hello二个函数
from function_sample_model import build_profile,hello
#这里不能用function_sample_model.函数
user_profile=build_profile('albert', 'einstein',
location='princeton',
field='physics',
field2='physics2')
print(user_profile)
13 使用as给函数指定别名
#使用as给函数指定别名
from function_sample_model import build_profile as bp,hello
user_profile=bp('albert', 'einstein',
location='princeton',
field='physics',
field2='physics2')
print(user_profile)
14 导入模块中的所有函数
#使用星号(*)导入所有函数,最好不要采用这种导入方法
from function_sample_model import *
user_profile=build_profile('albert', 'einstein',
location='princeton',
field='physics',
field2='physics2')
print(user_profile)