Bringing it all together

Introduction to Functions in Python

Hugo Bowne-Anderson

Instructor

Next exercises:

  • Generalized functions:

    • Count occurrences for any column

    • Count occurrences for an arbitrary number of columns

Introduction to Functions in Python

Add a default argument

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
Introduction to Functions in Python

Flexible arguments: *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
Introduction to Functions in Python

Let's practice!

Introduction to Functions in Python

Preparing Video For Download...