恭喜

Python 中级:面向开发者

Jasmin Ludolf

Senior Data Science Content Developer

第 1 章回顾

  • 内置函数:
    • print(), help(), type()
    • max(), min(), sum()
    • len(), round(), sorted()
  • 模块:
    • string, os

 

$$

  • 软件包:
    • pandas
Python 中级:面向开发者

第 2 章回顾

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

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

    # 返回 rounded_average 作为输出
    return rounded_average
Python 中级:面向开发者

第 2 章回顾

# 创建自定义函数
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 中级:面向开发者

第 2 章回顾

def average(values):
    """
    计算一组值的均值,并保留两位小数。

    参数:
        values (list): 数值列表。

    返回:
        rounded_average (float): 均值,保留两位小数。
    """

average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Python 中级:面向开发者

第 2 章回顾

# 使用任意位置参数
def average(*args):
    average_value = sum(args) / len(args)
    rounded_average = round(average_value, 2)
    return rounded_average

# 使用任意关键字参数 def average(**kwargs): average_value = sum(kwargs.values()) / len(kwargs.values()) rounded_average = round(average_value, 2) return rounded_average
Python 中级:面向开发者

第 3 章回顾

  • lambda arguments: expression
names = ["john", "sally", "leah"]

# 在 map() 中使用 lambda 函数
capitalize = map(lambda x: x.capitalize(), names)

# 转为列表
print(list(capitalize))
['John', 'Sally', 'Leah']
Python 中级:面向开发者

第 3 章回顾

ValueError 输出,确认字符串 'Hello' 不能转换为 float

$$

  • try
  • except
  • raise
Python 中级:面向开发者

下一步

  • 更多内置函数:

    • zip()
    • input()
    • reduce()
    • filter()
  • 更多软件包与模块:

    • time
    • venv
    • requests
    • fastapi

面向对象编程简介

  • 构建大规模软件的关键
Python 中级:面向开发者

恭喜!

Python 中级:面向开发者

Preparing Video For Download...