默认参数与关键字参数

Python 中级:面向开发者

Jasmin Ludolf

Senior Data Science Content Developer

平均值

# 创建自定义函数
def average(values):
    # 计算平均值
    average_value = sum(values) / len(values)

    # 四舍五入结果
    rounded_average = round(average_value, 2)

    # 返回 rounded_average 作为输出
    return rounded_average
  • values = 参数
Python 中级:面向开发者

参数

  • 传入给函数或方法的值
    • 位置
    • 关键字
Python 中级:面向开发者

位置参数

  • 按顺序提供参数,用逗号分隔
# 将圆周率保留 2 位小数
print(round(3.1415926535, 2))
3.14
Python 中级:面向开发者

关键字参数

  • 通过为关键字赋值来提供参数

  • 有助于理解与跟踪参数

# 将圆周率保留 2 位小数 
print(round(number=3.1415926535
Python 中级:面向开发者

关键字参数

  • 通过为关键字赋值来提供参数

  • 有助于理解与跟踪参数

# 将圆周率保留 2 位小数 
print(round(number=3.1415926535, ndigits=2))
3.14
Python 中级:面向开发者

识别关键字参数

# 获取关于 help 函数的更多信息
print(help(round))
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None.  Otherwise,
    the return value has the same type as the number.  ndigits may be negative.
Python 中级:面向开发者

关键字参数

Help on built-in function round in module builtins:

round(number, ndigits=None)
  Round a number to a given precision in decimal digits.

  The return value is an integer if ndigits is omitted or None. Otherwise 
  the return value has the same type as the number. ndigits may be negative.

$$

  • 第一个参数:number
  • 第二个参数:ndigits
Python 中级:面向开发者

默认参数

Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None.  Otherwise,
    the return value has the same type as the number.  ndigits may be negative.
  • None = 无值/空

  • 默认参数:为参数设置默认值的方式

  • None 改为 2

  • 常用值——用默认参数设置

Python 中级:面向开发者

添加参数

# 创建自定义函数
def average(values):
    average_value = sum(values) / len(values)
    rounded_average = round(average_value, 2)
    return rounded_average
Python 中级:面向开发者

添加参数

# 创建自定义函数
def average(values, rounded=False):


Python 中级:面向开发者

添加参数

# 创建自定义函数
def average(values, rounded=False):

# 若 rounded 为 True,则保留两位小数 if rounded == True:
average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Python 中级:面向开发者

添加参数

# 创建自定义函数
def average(values, rounded=False):

# 若 rounded 为 True,则保留两位小数 if rounded == True: average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
# 否则不四舍五入 else: average_value = sum(values) / len(values) return average_value
Python 中级:面向开发者

使用修改后的 average() 函数

# 准备时间列表(分钟)
preparation_times = [19.23, 15.67, 48.57, 23.45, 12.06, 34.56, 45.67]
Python 中级:面向开发者

使用修改后的 average() 函数

# 获取未四舍五入的平均值
print(average(preparation_times, False))
28.4585714
# 获取未四舍五入的平均值
print(average(preparation_times))
28.4585714
Python 中级:面向开发者

使用修改后的 average() 函数

# 获取四舍五入后的平均值
print(average(values=preparation_times, rounded=True))
28.46
Python 中级:面向开发者

Passons à la pratique !

Python 中级:面向开发者

Preparing Video For Download...