【Python基础学习】-函数

本文介绍了Python中函数的使用,包括定义函数、传递参数、返回值、处理列表、接收任意数量的实参以及将函数存储在模块中。详细讲解了如何定义带默认值和可变数量参数的函数,以及如何在函数中修改或禁止修改列表。同时,讨论了如何导入和使用模块中的函数,包括使用别名和导入所有函数的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

注:若使用这种语法,调用函数时就无需使用句点。由于我们在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 *
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值