สรุปทุกอย่างเข้าด้วยกัน

Python เบื้องต้น: การเขียนฟังก์ชัน

Hugo Bowne-Anderson

Instructor

แบบฝึกหัดถัดไป:

  • ฟังก์ชันแบบ Generalized:

    • นับจำนวนครั้งสำหรับคอลัมน์ใดก็ได้

    • นับจำนวนครั้งสำหรับคอลัมน์หลายคอลัมน์ตามต้องการ

Python เบื้องต้น: การเขียนฟังก์ชัน

การเพิ่ม 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
Python เบื้องต้น: การเขียนฟังก์ชัน

Argument แบบยืดหยุ่น: *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
Python เบื้องต้น: การเขียนฟังก์ชัน

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

Python เบื้องต้น: การเขียนฟังก์ชัน

Preparing Video For Download...