import pizza
from pizza import test2 as test3
from pizza import *
if __name__ == '__main__':
def gerrt_user():
print('你好我是函数')
gerrt_user()
def greet_user(username):
print('你好'+username)
greet_user('网聚信')
#传递实参
#位置实参
def describe_pet(animal_type, pet_name):
"""显示宠物的信息"""
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet('hamster', 'harry')
describe_pet('dog', 'willie')
#关键字实参
describe_pet(animal_type='hamster', pet_name='harry')
#默认值
'''编写函数时, 可给每个形参指定默认值 。 在调用函数中给形参提供了实参时, Python将使用指定的实参值; 否则, 将使用形参的默认值。 因此, 给形参指定默认值后, 可在函数
调用中省略相应的实参。 使用默认值可简化函数调用, 还可清楚地指出函数的典型用法。'''
def describe_pet(pet_name, animal_type='dog'):
"""显示宠物的信息"""
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet(pet_name='willie')
#等效的函数调用
# 一条名为Willie的小狗
describe_pet('willie')
describe_pet(pet_name='willie')
# 一只名为Harry的仓鼠
describe_pet('harry', 'hamster')
describe_pet(pet_name='harry', animal_type='hamster')
describe_pet(animal_type='hamster', pet_name='harry')
#避免实参错误
#等你开始使用函数后, 如果遇到实参不匹配错误, 不要大惊小怪。 你提供的实参多于或少于函数完成其工作所需的信息时, 将出现实参不匹配错误
#返回值
def get_formatted_name(first_name, last_name):
"""返回整洁的姓名"""
full_name = first_name + ' ' + last_name
return full_name.title()
musician = get_formatted_name('jimi', 'hendrix')
print(musician)
#让实参变成可选的
def get_formatted_name(first_name, last_name, middle_name=''):
"""返回整洁的姓名"""
if middle_name:
full_name = first_name + ' ' + middle_name + ' ' + last_name
else:
full_name = first_name + ' ' + last_name
return full_name.title()
musician = get_formatted_name('jimi', 'hendrix')
print(musician)
musician = get_formatted_name('john', 'hooker', 'lee')
print(musician)
#返回字典
def build_person(first_name, last_name):
"""返回一个字典, 其中包含有关一个人的信息"""
person = {'first': first_name, 'last': last_name}
return person
musician = build_person('jimi', 'hendrix')
print(musician)
#传递列表
def greet_users(names):
"""向列表中的每位用户都发出简单的问候"""
for name in names:
msg = "Hello, " + name.title() + "!"
print(msg)
usernames = ['hannah', 'ty', 'margot']
greet_users(usernames)
#在函数中修改列表
#禁止函数修改列表
#传递任意数量的实参
def make_pizza(*toppings):
"""打印顾客点的所有配料"""
print(toppings)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
#结合使用位置实参和任意数量实参
def make_pizza(size, *toppings):
"""概述要制作的比萨"""
print("\nMaking a " + str(size) +
"-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
#使用任意数量的关键字实参
def build_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
user_profile = build_profile('albert', 'einstein',location='princeton',field='physics')
print(user_profile)
#将函数存储在模块中
pizza.test(16, 'pepperoni')
#使用as 给函数指定别名
test3()
#导入模块中的所有函数
#使用星号( * ) 运算符可让Python导入模块中的所有函数
from pizza import test2 as test3
from pizza import *
if __name__ == '__main__':
def gerrt_user():
print('你好我是函数')
gerrt_user()
def greet_user(username):
print('你好'+username)
greet_user('网聚信')
#传递实参
#位置实参
def describe_pet(animal_type, pet_name):
"""显示宠物的信息"""
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet('hamster', 'harry')
describe_pet('dog', 'willie')
#关键字实参
describe_pet(animal_type='hamster', pet_name='harry')
#默认值
'''编写函数时, 可给每个形参指定默认值 。 在调用函数中给形参提供了实参时, Python将使用指定的实参值; 否则, 将使用形参的默认值。 因此, 给形参指定默认值后, 可在函数
调用中省略相应的实参。 使用默认值可简化函数调用, 还可清楚地指出函数的典型用法。'''
def describe_pet(pet_name, animal_type='dog'):
"""显示宠物的信息"""
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet(pet_name='willie')
#等效的函数调用
# 一条名为Willie的小狗
describe_pet('willie')
describe_pet(pet_name='willie')
# 一只名为Harry的仓鼠
describe_pet('harry', 'hamster')
describe_pet(pet_name='harry', animal_type='hamster')
describe_pet(animal_type='hamster', pet_name='harry')
#避免实参错误
#等你开始使用函数后, 如果遇到实参不匹配错误, 不要大惊小怪。 你提供的实参多于或少于函数完成其工作所需的信息时, 将出现实参不匹配错误
#返回值
def get_formatted_name(first_name, last_name):
"""返回整洁的姓名"""
full_name = first_name + ' ' + last_name
return full_name.title()
musician = get_formatted_name('jimi', 'hendrix')
print(musician)
#让实参变成可选的
def get_formatted_name(first_name, last_name, middle_name=''):
"""返回整洁的姓名"""
if middle_name:
full_name = first_name + ' ' + middle_name + ' ' + last_name
else:
full_name = first_name + ' ' + last_name
return full_name.title()
musician = get_formatted_name('jimi', 'hendrix')
print(musician)
musician = get_formatted_name('john', 'hooker', 'lee')
print(musician)
#返回字典
def build_person(first_name, last_name):
"""返回一个字典, 其中包含有关一个人的信息"""
person = {'first': first_name, 'last': last_name}
return person
musician = build_person('jimi', 'hendrix')
print(musician)
#传递列表
def greet_users(names):
"""向列表中的每位用户都发出简单的问候"""
for name in names:
msg = "Hello, " + name.title() + "!"
print(msg)
usernames = ['hannah', 'ty', 'margot']
greet_users(usernames)
#在函数中修改列表
#禁止函数修改列表
#传递任意数量的实参
def make_pizza(*toppings):
"""打印顾客点的所有配料"""
print(toppings)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
#结合使用位置实参和任意数量实参
def make_pizza(size, *toppings):
"""概述要制作的比萨"""
print("\nMaking a " + str(size) +
"-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
#使用任意数量的关键字实参
def build_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
user_profile = build_profile('albert', 'einstein',location='princeton',field='physics')
print(user_profile)
#将函数存储在模块中
pizza.test(16, 'pepperoni')
#使用as 给函数指定别名
test3()
#导入模块中的所有函数
#使用星号( * ) 运算符可让Python导入模块中的所有函数
pass
pizza.py
def test(size, *toppings):
"""概述要制作的比萨"""
print("\nMaking a " + str(size) +
"-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
def test2():
print('这是测试2')