預設與關鍵字引數

Intermediate Python for Developers

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 = 引數
Intermediate Python for Developers

引數

  • 傳入函式或方法的值
    • 位置
    • 關鍵字
Intermediate Python for Developers

位置引數

  • 依序提供引數,用逗號分隔
# Round pi to 2 digits
print(round(3.1415926535, 2))
3.14
Intermediate Python for Developers

關鍵字引數

  • 以指定 keywords 的方式提供引數

  • 便於理解與追蹤引數

# Round pi to 2 digits 
print(round(number=3.1415926535
Intermediate Python for Developers

關鍵字引數

  • 以指定 keywords 的方式提供引數

  • 便於理解與追蹤引數

# Round pi to 2 digits 
print(round(number=3.1415926535, ndigits=2))
3.14
Intermediate Python for Developers

辨識關鍵字引數

# 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.
Intermediate Python for Developers

關鍵字引數

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
Intermediate Python for Developers

預設引數

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」值的方式

  • None 改為 2

  • 常用值可用預設引數設定

Intermediate Python for Developers

新增引數

# Create a custom function
def average(values):
    average_value = sum(values) / len(values)
    rounded_average = round(average_value, 2)
    return rounded_average
Intermediate Python for Developers

新增引數

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


Intermediate Python for Developers

新增引數

# 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
Intermediate Python for Developers

新增引數

# 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
Intermediate Python for Developers

使用修改後的 average() 函式

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

使用修改後的 average() 函式

# Get the average without rounding
print(average(preparation_times, False))
28.4585714
# Get the average without rounding
print(average(preparation_times))
28.4585714
Intermediate Python for Developers

使用修改後的 average() 函式

# Get the rounded average
print(average(values=preparation_times, rounded=True))
28.46
Intermediate Python for Developers

一起來練習吧!

Intermediate Python for Developers

Preparing Video For Download...