기본 및 키워드 인수

개발자를 위한 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 중급

인수

  • 함수 또는 메서드에 입력한 값
    • 위치
    • 키워드
개발자를 위한 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에 지정하는 방법

  • None2로 덮어씀

  • 일반적으로 사용되는 값 - 기본 인수로 설정

개발자를 위한 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...