0
点赞
收藏
分享

微信扫一扫

【Python基础学习】-函数

独西楼Q 2022-01-24 阅读 122

1. 定义函数

定义打印问候语的简单函数,名为greet_user():

def greet_user(): 
    """显示简单的问候语"""
    print("Hello!")
    
greet_user() 

1.1 实参和形参

定义函数greet_user()时,括号中指定的值就是“形参”
调用函数greet_user()时,括号中指定的值就是“实参”

def greet_user(形参): 
    """显示简单的问候语""" 
    print("Hello, " + 形参.title() + "!") 

greet_user('实参') 

2 返回值

函数返回的值被称为返回值。在函数中,可使用return语句将值返回到调用函数的代码行。

2.1 返回简单值

定义函数get_formatted_name()接受名和姓并返回整洁的姓名,通过return语句返回full_name值

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)  

2.2 让实参变成可选的

给实参middle_name指定一个默认值“空字符串”,并在用户没有提供中间名时不使用这个实参

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()

"""不使用middle_name实参"""
musician = get_formatted_name('jimi', 'hendrix')
print(musician)
"""使用middle_name实参"""
musician = get_formatted_name('john', 'hooker', 'lee')
print(musician)

2.3 返回字典

函数可返回任何类型的值,包括列表和字典等较复杂的数据结构,例如字典:

def build_person(first_name, last_name):
    """返回一个字典,其中包含有关一个人的信息"""
    person = {'first': first_name, 'last': last_name}
    return person

musician = build_person('jimi', 'hendrix')
print(musician)

2.4 结合使用函数和 while 循环

下面将结合使用函数 get_formatted_name()和while循环,并提供break语句提供退出循环的途径,以更正规的方式问候用户

while True:
    print("\nPlease tell me your name: ")
    print("(enter 'q' at any time to quit)")
    f_name = input("First name: ")
    if f_name == 'q':
        break
    l_name = input("Last name: ")
    if l_name == "q":
        break
    formatted_name = get_formatted_name(f_name, l_name)
    print("\nHello," + formatted_name + "!")

3. 传递列表

假设有一个用户列表,我们要问候其中的每位用户。下面的示例将一个名字列表传递给greet_users()函数,并使用for循环遍历问候列表中的每个人:

def greet_users(names):
    """向列表中的每位用户都发出简单的问候"""
    for name in names:
        msg = "Hello, " + name.title() + "!"
        print(msg)

usernames = ['hannah', 'ty', 'margot']
greet_users(usernames)

3.1 在函数中修改列表

来看一家为用户提交的设计制作3D打印模型的公司。需要打印的设计存储在一个列表中, 打印后移到另一个列表中

def print_models(unprinted_designs, completed_models):
    """
    模拟打印每个设计,直到没有未打印的设计为止
    打印每个设计后,都将其移到列表completed_models中
    """
    while unprinted_designs:
        current_design = unprinted_designs.pop()
    # 模拟根据设计制作3D打印模型的过程
        print("Printing model: " + current_design)
        completed_models.append(current_design)


def show_completed_models(completed_models):
    """显示打印好的所有模型"""
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)


unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []

print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)

3.2 禁止函数修改列表

有时候即便打印所有列表后,也要保留原来的列表。
在print_models.py中,如果不想清空未打印的设计列表, 可像下面这样调用print_models()

print_models(unprinted_designs, completed_models)

替换为

print_models(unprinted_designs[:], completed_models)

4. 传递任意数量的实参

有时候,你预先不知道函数需要接受多少个实参,可以在形参前加“*”,不管提供多少实参,都可以收入囊中

def make_pizza(*toppings):
    """概述要制作的比萨"""
    print("\nMaking a pizza with the following toppings:")
    for topping in toppings:
        print("- " + topping)


make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')

4.1 使用任意数量的关键字实参

有时候,需要接受任意数量的实参,但预先不知道传递给函数的会是什么样的信息。在这种情况下,可以在形参前加“**”,将函数编写成能够接受任意数量的键—值对——调用语句提供了多少就接受多少。

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)

5. 将函数存储在模块中

函数的优点之一是,使用它们可将代码块与主程序分离,import语句允许在当前运行的程序文件中使用模块中的代码

5.1 导入整个模块

在demo_01.py中创建一个包含函数make_pizza()的模块

def make_pizza(size, *toppings):
    """概述要制作的比萨"""
    print("\nMaking a " + str(size) + "-inch pizza with the following toppings:")
    for topping in toppings:
        print("- " + topping)
 在demo_01.py同目录下demo_02.py中调用make_pizza()
import demo_01

demo_01.make_pizza(16, 'pepperoni')
demo_01.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

5.2 导入模块下的特定函数

from demo_01 import make_pizza

5.3 使用as给模块、函数指定别名

from demo_01 import make_pizza as mp
import demo_01 as mn

5.4 导入模块中的所有函数
使用星号(*)运算符可让Python导入模块中的所有函数

from demo_01 import *
举报

相关推荐

0 条评论