0Pricing
Python For Kids · 课时

参数与返回值

学习向函数传入数据并获取结果。

参数与返回值 是 CoddyKit 上的免费 Python For Kids 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Python For Kids 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Python For Kids 课程共包含 4 节课。

向函数传递数据

实参与返回值

欢迎回到函数课程!今天我们将学习如何使用实参向函数传入数据,以及如何使用返回值获取结果。

参数与返回值 — 插图 1

什么是函数实参?

实参是您传入函数的值,用于提供函数执行任务所需的数据。它可以让函数更加灵活且易于复用。

  • 形参:列在函数括号内的变量。
  • 实参:调用函数时传入的值。

让我们看看实参在 Python 函数中如何工作。

在函数中使用实参

定义函数时,您可以指定形参,它们会作为调用函数时传入实参的占位符。

示例:

# Defines a function that greets a user by name
def greet(name):
    greeting = "Hello, " + name + "!"  # Creates a greeting message
    print(greeting)  # Prints the greeting message

# Calls the function with the argument "Alice"
greet("Alice")  # Output: Hello, Alice!

默认实参

您可以为函数参数指定默认值。如果调用函数时没有为该参数提供实参,就会使用默认值。

示例:

# Defines a function with a default parameter
def greet(name="Python Learner"):
    greeting = "Hello, " + name + "!"  # Creates a greeting message
    print(greeting)  # Prints the greeting message

# Calls the function without an argument
greet()  # Output: Hello, Python Learner!

# Calls the function with an argument
greet("Bob")  # Output: Hello, Bob!

关键字实参

调用函数时,您可以通过参数名称指定实参,这样就可以按任意顺序传入它们。

示例:

# Defines a function that describes a person
def describe_person(name, age):
    description = name + " is " + str(age) + " years old."  # Creates a description
    print(description)  # Prints the description

# Calls the function using keyword arguments
describe_person(age=25, name="Charlie")  # Output: Charlie is 25 years old.

可变长度实参(*args)

有时,您可能不知道函数会接收多少个实参。您可以使用 *args 处理任意数量的位置实参。

示例:

# Defines a function that sums any number of numbers
def sum_numbers(*args):
    total = 0  # Initializes the total
    for number in args:
        total += number  # Adds each number to the total
    return total  # Returns the sum

# Calls the function with different numbers of arguments
print(sum_numbers(1, 2, 3))       # Output: 6
print(sum_numbers(4, 5))          # Output: 9
print(sum_numbers(10, 20, 30, 40))  # Output: 100

可变长度关键字实参(**kwargs)

与 *args 类似,**kwargs 可以帮助您处理任意数量的关键字实参(即命名实参)。

示例:

# Defines a function that prints keyword arguments
def print_info(**kwargs):
    for key, value in kwargs.items():
        print(key + ": " + str(value))  # Prints each key-value pair

# Calls the function with various keyword arguments
print_info(name="Diana", age=28, city="New York")
# Output:
# name: Diana
# age: 28
# city: New York

返回值

return 语句会将一个值从函数传回调用方。这样,您就可以在代码的其他部分使用函数的结果。

示例:

# Defines a function that returns the square of a number
def square(number):
    result = number ** 2  # Calculates the square
    return result  # Returns the square

# Calls the function and stores the returned value
squared_value = square(5)
print("Square:", squared_value)  # Output: Square: 25

# Function that multiplies two numbers and returns the result
def multiply(a, b):
    product = a * b  # Calculates the product
    return product  # Returns the product

# Calls the function and stores the returned value
result = multiply(4, 5)
print("Product:", result)

做得很好!

您已经学会了如何在 Python 中向函数传入实参,并使用返回值获取结果。这些概念可以让函数更加灵活和强大,从而处理动态数据并执行复杂任务。请继续练习:创建带有不同类型实参和返回值的函数!

参数与返回值 — 插图 10

常见问题解答

「参数与返回值」课时是免费的吗?

是的 — 「参数与返回值」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Python For Kids 课程的其余内容,请升级到 CoddyKit PRO。 Python For Kids 课程共包含 4 节课。

「参数与返回值」这节课中我会学到什么?

学习向函数传入数据并获取结果。 你通过在浏览器中直接运行的动手代码来练习 Python For Kids,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Python For Kids 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Python For Kids 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「参数与返回值」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Python For Kids 课中编写并运行代码吗?

能。每节 Python For Kids 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 什么是函数
  2. 定义自定义函数
  3. 参数与返回值
  4. Lambda 函数
← 返回 Python For Kids