```python
```python def greet(name): """ Greets a person by name.
Args: name: The name of the person to greet.
Returns: A greeting message. """ return f"Hello, {name}!"
def add(x, y): """ Adds two numbers together.
Args: x: The first number. y: The second number.
Returns: The sum of x and y. """ return x + y
def factorial(n): """ Calculates the factorial of a non-negative integer.
Args: n: The non-negative integer.
Returns: The factorial of n. """ if n == 0: return 1 else: return n * factorial(n - 1)
if name == "main": print(greet("World")) print(add(2, 3)) print(factorial(5)) ```
文件说明
该文件包含三个 Python 函数:
greet(name)
:接受一个姓名作为参数,并返回一个问候语。add(x, y)
:接受两个数字作为参数,并返回它们的和。factorial(n)
:接受一个非负整数作为参数,并返回该整数的阶乘。
该文件还包含一个简单的示例,演示了如何使用这些函数。
函数文档
每个函数都有一个文档字符串,描述了它的功能、参数和返回值。
示例
```python
greet("Alice") 'Hello, Alice!'
add(2, 3) 5
factorial(5) 120 ```
- 本文标签: Python
- 本文链接: https://blog.sandy1029.cloud/article/308
- 版权声明: 本文由nisan原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权