Definiera en anpassad funktion

Intermediate Python för utvecklare

Jasmin Ludolf

Senior Data Science Content Developer

Beräkna medelvärdet

# 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 för utvecklare

När ska du skapa en anpassad funktion?

Upprepa inte dig själv (DRY)

$$

  • Att tänka på när du skapar en anpassad funktion:
    • Antal rader
    • Kodens komplexitet
    • Hur ofta koden används

Öken

Intermediate Python för utvecklare

Skapa en anpassad funktion

# Create a custom function to calculate the average value
def








Intermediate Python för utvecklare

Skapa en anpassad funktion

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








Intermediate Python för utvecklare

Skapa en anpassad funktion

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








Intermediate Python för utvecklare

Skapa en anpassad funktion

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








  • values (argument) – information som funktionen behöver för att utföra sitt arbete
Intermediate Python för utvecklare

Skapa en anpassad funktion

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








Intermediate Python för utvecklare

Skapa en anpassad funktion

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





Intermediate Python för utvecklare

Skapa en anpassad funktion

# 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 för utvecklare

Skapa en anpassad funktion

# 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 och rounded_average är bara tillgängliga inom average()
Intermediate Python för utvecklare

Skapa en anpassad funktion

# 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
  • Dokumentation är viktigt 📚
Intermediate Python för utvecklare

Använda en anpassad funktion

# 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 för utvecklare

Spara en funktions utdata

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

print(average_time)
28.46
Intermediate Python för utvecklare

Nu kör vi en övning!

Intermediate Python för utvecklare

Preparing Video For Download...