การสร้างฟังก์ชันแบบกำหนดเอง

Python ระดับกลางสำหรับนักพัฒนา

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
Python ระดับกลางสำหรับนักพัฒนา

เมื่อไหร่ควรสร้างฟังก์ชันแบบกำหนดเอง

อย่าเขียนโค้ดซ้ำ (DRY)

$$

  • สิ่งที่ควรพิจารณาก่อนสร้างฟังก์ชันแบบกำหนดเอง:
    • จำนวนบรรทัด
    • ความซับซ้อนของโค้ด
    • ความถี่ในการใช้งาน

ทะเลทราย

Python ระดับกลางสำหรับนักพัฒนา

การสร้างฟังก์ชันแบบกำหนดเอง

# Create a custom function to calculate the average value
def








Python ระดับกลางสำหรับนักพัฒนา

การสร้างฟังก์ชันแบบกำหนดเอง

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








Python ระดับกลางสำหรับนักพัฒนา

การสร้างฟังก์ชันแบบกำหนดเอง

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








Python ระดับกลางสำหรับนักพัฒนา

การสร้างฟังก์ชันแบบกำหนดเอง

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








  • values (อาร์กิวเมนต์) - ข้อมูลที่ฟังก์ชันต้องการเพื่อทำงาน
Python ระดับกลางสำหรับนักพัฒนา

การสร้างฟังก์ชันแบบกำหนดเอง

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








Python ระดับกลางสำหรับนักพัฒนา

การสร้างฟังก์ชันแบบกำหนดเอง

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





Python ระดับกลางสำหรับนักพัฒนา

การสร้างฟังก์ชันแบบกำหนดเอง

# 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)



Python ระดับกลางสำหรับนักพัฒนา

การสร้างฟังก์ชันแบบกำหนดเอง

# 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 และ rounded_average ใช้งานได้เฉพาะภายใน average() เท่านั้น
Python ระดับกลางสำหรับนักพัฒนา

การสร้างฟังก์ชันแบบกำหนดเอง

# 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 เป็นสิ่งสำคัญ 📚
Python ระดับกลางสำหรับนักพัฒนา

การใช้ฟังก์ชันแบบกำหนดเอง

# 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
Python ระดับกลางสำหรับนักพัฒนา

การเก็บผลลัพธ์ของฟังก์ชัน

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

print(average_time)
28.46
Python ระดับกลางสำหรับนักพัฒนา

มาฝึกกันเถอะ!

Python ระดับกลางสำหรับนักพัฒนา

Preparing Video For Download...