Defining a custom function

Intermediate Python for Developers

George Boorman

Curriculum Manager, DataCamp

Calculating the average

# Sales variable
sales = [125.97, 84.32, 99.78, 154.21, 78.50, 83.67, 111.13]

# Calculating average sales average_sales = sum(sales) / len(sales)
# Rounding the results rounded_average_sales = round(average_sales, 2) print(rounded_average_sales)
105.37
Intermediate Python for Developers

Create a custom function

  • Considerations for making a custom function:
    • Number of lines
    • Code complexity
    • Frequency of use
    • Don't Repeat Yourself (DRY)

Desert

Intermediate Python for Developers

Creating a custom function

# Create a custom function to calculate the average value
def








Intermediate Python for Developers

Creating a custom function

# Create a custom function to calculate the average value
def average








Intermediate Python for Developers

Creating a custom function

# Create a custom function to calculate the average value
def average(








Intermediate Python for Developers

Creating a custom function

# Create a custom function to calculate the average value
def average(values)








Intermediate Python for Developers

Creating a custom function

# Create a custom function to calculate the average value
def average(values):








Intermediate Python for Developers

Creating a custom function

# Create a custom function to calculate the average value
def average(values):
    # Calculate the average
    average_value = sum(values) / len(values)





Intermediate Python for Developers

Creating a custom function

# Create a custom function to calculate the average value
def average(values):
    # Calculate the average
    average_value = sum(values) / len(values)

    # Round the results
    rounded_average = round(average_value, 2)



Intermediate Python for Developers

Creating a custom function

# Create a custom function to calculate the average value
def average(values):
    # Calculate the average
    average_value = sum(values) / len(values)

    # Round the results
    rounded_average = round(average_value, 2)

# Return an output return
Intermediate Python for Developers

Creating a custom function

# Create a custom function to calculate the average value
def average(values):
    # Calculate the average
    average_value = sum(values) / len(values)

    # Round the results
    rounded_average = round(average_value, 2)

    # Return rounded_average as an output
    return rounded_average
Intermediate Python for Developers

Returning a calculation

# Create a custom function to calculate the average value
def average(values):
    # Calculate the average
    average_value = sum(values) / len(values)

    # Return the rounded results
    return round(average_value, 2)
Intermediate Python for Developers

Using a custom function

sales = [125.97, 84.32, 99.78, 154.21, 78.50, 83.67, 111.13]

# Calculating the average average(sales)
105.37
Intermediate Python for Developers

Storing a function's output

# Calculating the average
average(sales)
105.37
# Storing average_sales
average_sales = average(sales)
print(average_sales)
105.37
Intermediate Python for Developers

Let's practice!

Intermediate Python for Developers

Preparing Video For Download...