综合运用

Python 函数入门

Hugo Bowne-Anderson

Instructor

下一步练习:

  • 泛化函数:

    • 统计任意列的出现次数

    • 统计任意数量列的出现次数

Python 函数入门

添加默认参数

def power(number, pow=1):
    """Raise number to the power of pow."""
    new_value = number ** pow
    return new_value
power(9, 2)
81
power(9)
9
Python 函数入门

可变参数:*args(1)

def add_all(*args):
    """Sum all values in *args together."""

    # Initialize sum
    sum_all = 0

    # Accumulate the sum
    for num in args:
        sum_all = sum_all + num

    return sum_all
Python 函数入门

让我们练习!

Python 函数入门

Preparing Video For Download...