[Python]함수의 인자

함수의 인자를 넘기는 방법에 대해 알아보자

# 예제1
def greet(name, msg="Good morning!"):
    return "Hi! " + name + ', ' + msg

# 실행
print(greet("Kim"))
print(greet("Park", "How do you do?"))
print()

# 예제2
def add1(a, b=10, c=15):
    return a + b + c

print(f'add = {add1(1)}')
print(f'add = {add1(1, 20)}')
print(f'add = {add1(1, 30, 50)}')
print(f'add = {add1(b=1, a=30, c=50)}')
print()

# 예제3
def add2(*d):
    tot = 0
    for i in d:                
        tot += i
    return tot
        

print(f'add2 = {add2(*(i for i in range(1, 101)))}')

## 결과
Hi! Kim, Good morning!
Hi! Park, How do you do?

add = 26
add = 36
add = 81
add = 81

add2 = 5050

You may also like...

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다