8.1 函数定义
def sayhello () :
print '你好'
sayhello () ;
输出:你好
8.1.1 形参和实参
形参:函数完成工作所需要的信息 实参:具体的值
# 形参:name 实参:summer
def hello(name):
print(f"hello,{name.title()}")
hello('summer')
输出:hello,Summer
8.2 位置实参 注:传参时必须按照顺序
def hobby(type,name):
print(f"my favorite {type} is {name}")
hobby("旅行","北京")
输出:my favorite 旅行 is 北京
8.2.1 关键字实参
def descripe(pet_name,animal_type='dog'):
print(f"I have a {animal_type}")
print(f"{animal_type} name is {pet_name.title()} ")
descripe(pet_name="花花")
输出:
I have a dog
dog name is 花花
8.2.2 默认值
def describe_pet(pet_name,animal_type='dog'):
print(f"I have a {animal_type}")
print(f"{animal_type} name is {pet_name.title()}")
describe_pet(pet_name="花花")
输出:
I have a dog
dog name is 花花
8.2.3 让实参变为可选的
def get_name(first_name,last_name,middle_name=''):
if middle_name:
full_name =f"{first_name}{middle_name}{last_name}"
else:
full_name =f"{first_name}{last_name}"
return full_name.title()
res = get_name('a','b')
print(res)
res1 = get_name('a','b','c')
print(res1)
输出:
Ab
Acb
8.3 返回字典
def build_person(first_name,last_name,age=None):
person = {'first':first_name,'last':last_name}
if age:
person['age'] = age
return person
res = build_person("牛","亚宁")
print(res)
输出:
{'first': '牛', 'last': '亚宁'}
8.4 结合使用函数和while循环
def get_name(first_name,last_name):
full_name = (f"{first_name},{last_name}")
return full_name
while True:
f_name =input("请输入你的first_name:")
if f_name =='q':
break
l_name =input("请输入你的last_name:")
if l_name =='q':
break
f_name = get_name(f_name,l_name)
print(f"\n Hello\n{f_name}")
8.5 传递列表
def greet_users(names):
for namein names:
msg =f"Hello,{name.title()}"
print(msg)
usernames = ['summer','michael','andrew']
greet_users(usernames)
输出:
Hello,Summer
Hello,Michael
Hello,Andrew
8.5.1 在函数中修改列表
例1:
old_fruit = ['banbana','orange','apple']
new_fruit = []
while old_fruit:
fruit = old_fruit.pop()
print(f"{fruit}")
new_fruit.append(fruit)
for nin new_fruit:
print(n)
输出:
apple
orange
banbana
apple
orange
banbana
例子2:
def print_models(unprinted_designs,complate_models):
while unprinted_designs:
current_design = unprinted_designs.pop()
print(f"{current_design}")
complate_models.append(current_design)
def show_complated_models(complate_models):
for complate_modelin complate_models:
print(complate_model)
unprinted_designs = ['phone case','robot pendant','dode']
complate_models = []
print_models(unprinted_designs,complate_models)
show_complated_models(complate_models)
输出:
dode
robot pendant
phone case
dode
robot pendant
phone case
8.6 传递任意数量的形参
def car(size,*topping):
print(size)
print(topping)
car(12,'宝马')
输出:
12
('宝马',)
8.7 使用任意数量的关键字实参
def information(first_name,last_name,**userinfo):
userinfo['first_name'] = first_name
userinfo['last_name'] = last_name
return userinfo
user_information = information(
'summer','michael',
location ='北京',
sex ='女'
)
print(user_information)
输出:
{'location': '北京', 'sex': '女', 'first_name': 'summer', 'last_name': 'michael'}
8.8 结合使用位置实参和任意数量实参
def describe_pet(pet_name,animal_type='dog'):
print(f"I have a {animal_type}")
print(f"{animal_type} name is {pet_name.title()}")
describe_pet(pet_name="花花")
输出:
I have a dog
dog name is 花花
模块导入
导入整个模块: import pizza
导入特定的函数:from pizza import function_name
使用as给函数指定别名:from pizza import function_name as fn
使用as给模块指定别名:import pizza as pa
导入模块中的所有函数:
from pizza import *