อาร์กิวเมนต์ค่าเริ่มต้นและอาร์กิวเมนต์แบบคีย์เวิร์ด

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

Jasmin Ludolf

Senior Data Science Content Developer

ค่าเฉลี่ย

# Create a custom function
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
  • values = อาร์กิวเมนต์
Python ระดับกลางสำหรับนักพัฒนา

อาร์กิวเมนต์

  • ค่าที่ส่งให้ฟังก์ชันหรือเมธอด
    • แบบตำแหน่ง (Positional)
    • แบบคีย์เวิร์ด (Keyword)
Python ระดับกลางสำหรับนักพัฒนา

อาร์กิวเมนต์แบบตำแหน่ง

  • ระบุอาร์กิวเมนต์ตามลำดับ คั่นด้วยเครื่องหมายจุลภาค
# Round pi to 2 digits
print(round(3.1415926535, 2))
3.14
Python ระดับกลางสำหรับนักพัฒนา

อาร์กิวเมนต์แบบคีย์เวิร์ด

  • ระบุอาร์กิวเมนต์โดยกำหนดค่าให้ keywords

  • ช่วยให้อ่านและติดตามอาร์กิวเมนต์ได้ง่ายขึ้น

# Round pi to 2 digits 
print(round(number=3.1415926535
Python ระดับกลางสำหรับนักพัฒนา

อาร์กิวเมนต์แบบคีย์เวิร์ด

  • ระบุอาร์กิวเมนต์โดยกำหนดค่าให้ keywords

  • ช่วยให้อ่านและติดตามอาร์กิวเมนต์ได้ง่ายขึ้น

# Round pi to 2 digits 
print(round(number=3.1415926535, ndigits=2))
3.14
Python ระดับกลางสำหรับนักพัฒนา

การระบุอาร์กิวเมนต์แบบคีย์เวิร์ด

# Get more information about the help function
print(help(round))
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None.  Otherwise,
    the return value has the same type as the number.  ndigits may be negative.
Python ระดับกลางสำหรับนักพัฒนา

อาร์กิวเมนต์แบบคีย์เวิร์ด

Help on built-in function round in module builtins:

round(number, ndigits=None)
  Round a number to a given precision in decimal digits.

  The return value is an integer if ndigits is omitted or None. Otherwise 
  the return value has the same type as the number. ndigits may be negative.

$$

  • อาร์กิวเมนต์แรก: number
  • อาร์กิวเมนต์ที่สอง: ndigits
Python ระดับกลางสำหรับนักพัฒนา

อาร์กิวเมนต์ค่าเริ่มต้น

Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.

    The return value is an integer if ndigits is omitted or None.  Otherwise,
    the return value has the same type as the number.  ndigits may be negative.
  • None = ไม่มีค่า / ว่างเปล่า

  • อาร์กิวเมนต์ค่าเริ่มต้น: การกำหนดค่า default ให้กับ argument

  • เราแทนที่ None ด้วย 2

  • ค่าที่ใช้บ่อย — กำหนดด้วยอาร์กิวเมนต์ค่าเริ่มต้น

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

การเพิ่มอาร์กิวเมนต์

# Create a custom function
def average(values):
    average_value = sum(values) / len(values)
    rounded_average = round(average_value, 2)
    return rounded_average
Python ระดับกลางสำหรับนักพัฒนา

การเพิ่มอาร์กิวเมนต์

# Create a custom function
def average(values, rounded=False):


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

การเพิ่มอาร์กิวเมนต์

# Create a custom function
def average(values, rounded=False):

# Round average to two decimal places if rounded is True if rounded == True:
average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
Python ระดับกลางสำหรับนักพัฒนา

การเพิ่มอาร์กิวเมนต์

# Create a custom function
def average(values, rounded=False):

# Round average to two decimal places if rounded is True if rounded == True: average_value = sum(values) / len(values) rounded_average = round(average_value, 2) return rounded_average
# Otherwise, don't round else: average_value = sum(values) / len(values) return average_value
Python ระดับกลางสำหรับนักพัฒนา

การใช้ฟังก์ชัน average() ที่ปรับปรุงแล้ว

# List of preparation times (minutes)
preparation_times = [19.23, 15.67, 48.57, 23.45, 12.06, 34.56, 45.67]
Python ระดับกลางสำหรับนักพัฒนา

การใช้ฟังก์ชัน average() ที่ปรับปรุงแล้ว

# Get the average without rounding
print(average(preparation_times, False))
28.4585714
# Get the average without rounding
print(average(preparation_times))
28.4585714
Python ระดับกลางสำหรับนักพัฒนา

การใช้ฟังก์ชัน average() ที่ปรับปรุงแล้ว

# Get the rounded average
print(average(values=preparation_times, rounded=True))
28.46
Python ระดับกลางสำหรับนักพัฒนา

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

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

Preparing Video For Download...