自訂函式的定義

Intermediate Python for Developers

Jasmin Ludolf

Senior Data Science Content Developer

計算平均值

# List of preparation times (minutes)
preparation_times = [19.23, 15.67, 48.57, 23.45, 12.06, 34.56, 45.67]

# Calculating average preparation time average_time = sum(preparation_times) / len(preparation_times)
# Rounding the results rounded_average_time = round(average_time, 2) print(average_time)
28.46
Intermediate Python for Developers

何時建立自訂函式

Don't Repeat Yourself (DRY)

$$

  • 何時考慮做成自訂函式:
    • 程式行數
    • 程式碼複雜度
    • 使用頻率

沙漠

Intermediate Python for Developers

建立自訂函式

# Create a custom function to calculate the average value
def








Intermediate Python for Developers

建立自訂函式

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








Intermediate Python for Developers

建立自訂函式

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








Intermediate Python for Developers

建立自訂函式

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








  • values (argument) - information the function needs to do its job
Intermediate Python for Developers

建立自訂函式

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








Intermediate Python for Developers

建立自訂函式

# 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

建立自訂函式

# 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

建立自訂函式

# 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
  • average_value and rounded_average are only available within average()
Intermediate Python for Developers

建立自訂函式

# 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
  • Documentation is essential 📚
Intermediate Python for Developers

使用自訂函式

# List of preparation times (minutes)
preparation_times = [19.23, 15.67, 48.57, 23.45, 12.06, 34.56, 45.67]

# Calculating the average print(average(preparation_times))
28.46
# List of orders
orders = [12, 8, 10, 9, 15, 21, 16]

print(average(orders))
12.86
Intermediate Python for Developers

儲存函式輸出

# Calculating the average
print(average(preparation_times))
28.46
# Storing average_time
average_time = average(preparation_times)

print(average_time)
28.46
Intermediate Python for Developers

一起來練習吧!

Intermediate Python for Developers

Preparing Video For Download...