반응형
SMALL
14. 함수
def 함수명(매개변수):
수행할 문장
return 반환값
14.1 매개변수 개수 미지정 함수
def func_name(*args):
result = 0
for i in args:
result = result + i
return result
result = add_many(1,2,3)
print(result)
>> 6
result = add_many(1,2,3,4,5,6,7,8,9,10)
print(result)
>> 55
def func_name(stringValue, *args):
14.2 키워드 파라미터 kwargs
**
def print_kwargs(**kwargs):
print(kwargs)
pring_kwargs(a=1)
>> {'a':1} # 딕셔너리 형태로 출력
print_kwargs(name='foo', age=3)
>> {'age': 3, 'name': 'foo'}
14.3 여러 값 반환
def add_and_mul(a,b):
return a+b, a*b # 값 2개
result = add_and_mul(3,4)
result
>> (7, 12) # 값 2개가 튜플로 반환 됨
result1, result2 = add_and_mul(3,4)
result1
>> 7
result2
>> 12
14.4 매개변수 초기화
def func(name, age, woman=True) (O)
def func(name, woman=True, age) (X)
# 초기화 한 매개변수 뒤에 초기화하지 않은 매개변수 올 수 없음
14.5 lamda
def
와 같은 역할- return 없어도 결과값 반환
add = lambda a, b: a+b
result = add(3, 4)
print(result)
>> 7
반응형
LIST
'Python' 카테고리의 다른 글
[python] 파일 입출력 (0) | 2020.09.05 |
---|---|
[python] 사용자 입력 (0) | 2020.09.05 |
[python] for 문 (0) | 2020.09.03 |
[python] while 문 (0) | 2020.09.03 |
[python] if 문 (0) | 2020.09.03 |