函数
def describe_pet(pet_name,animal_type ='dog'): # 形参可以指定默认值
print("I have a "+ animal_type +".")
print("My " + animal_type + "'s name is " + pet_name.title()+".")
# 有如下几种方式调用上面的函数
describe_pet('whillie')
describe_pet(pet_name='whillie')
describe_pet('harry','hamster') # 使用位置实参来调用函数时,要注意顺序和形参对应
describe_pet(pet_name='harry',animal_type='hamster') # 使用关键字实参是传递给函数的名称-值对,关键字实参的顺序无关紧要,如下一行
describe_pet(animal_type='hamster',pet_name='harry')
I have a dog.
My dog's name is Whillie.
I have a dog.
My dog's name is Whillie.
I have a hamster.
My hamster's name is Harry.
I have a hamster.
My hamster's name is Harry.
I have a hamster.
My hamster's name is Harry.
传递列表给形参,一种情况是可以修改列表,一种情况是禁止函数修改列表。
def print_models(unprinted_designs,completed_models):
while unprinted_designs:
current_designs = unprinted_designs.pop()
completed_models.append(current_designs)
unprinted_designs = ['iphone case','robot pendant','dodecahedron']
completed_models = []
print_models(unprinted_designs,completed_models)
print("unprinted_designs:",unprinted_designs)
print("completed_models:",completed_models)
print("------------------------------------------------")
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs[:], completed_models) # 用切片表示法创建列表的副本,这样可以保留原始列表的内容,但也增加了创建列表副本的时间和内存
print("unprinted_designs:",unprinted_designs)
print("completed_models:",completed_models)
unprinted_designs: []
completed_models: ['dodecahedron', 'robot pendant', 'iphone case']
------------------------------------------------
unprinted_designs: ['iphone case', 'robot pendant', 'dodecahedron']
completed_models: ['dodecahedron', 'robot pendant', 'iphone case']
返回字典
def build_person(first_name,last_name):
person = {'first':first_name,'last':last_name}
return person
musician = build_person('jimi','hendrix')
print(musician)
{'first': 'jimi', 'last': 'hendrix'}
传递任意数量的实参
python先匹配位置实参和关键字实参,再将余下的实参都收集到最后一个形参中。
# 结合使用位置实参和任意数量实参
def make_pizza(size,*toppings): # 形参名*toppings中的星号让python创建一个名为toppings的空元组,并将收到的所有值都封装到这个元组中
print("making a " + str(size) + "-inch pizza with the following toppings:")
for toppiing in toppings:
print("- "+ toppiing)
make_pizza(16,'pepperoni')
make_pizza(12,'mushrooms','green peppers','extra cheese')
print("------------------------------------------------------")
# 使用任意数量的关键字实参
def bulid_profile(first,last,**user_info): # **user_info中的两个星号让python创建一个名为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 = bulid_profile('albert','einstein',
location = 'princeton',
field = 'physics')
print(user_profile)
making a 16-inch pizza with the following toppings:
- pepperoni
making a 12-inch pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese
------------------------------------------------------
{'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton', 'field': 'physics'}
将函数存储在模块(文件)中
impot module_name # 导入整个模块
module_name.function_name() # 使用模块中的函数
from module_name import function_0,function_1,function_2 # 导入特定的函数
function_0() # 直接使用函数即可,不用再加模块名
# 导入模块中的所有函数
from module_name import *
# 使用as给函数指定别名
from module_name import function_name as fn
# 使用as给模块指定别名
import module_name as mn
以上内容为作者总结自参考书籍《python编程入门到实践》,希望对你有所帮助。