# 1. 위치 매개변수
# 가장 기본적인 매개변수
def my_func(a,b):
print(a,b)
my_func(2,3)
# 2. 기본 매개변수
# 매개변수의 기본값을 지정할 수 있다.
def post_info(title, content='내용없음'):
print('제목 :', title)
print('내용 :', content)
post_info('출석합니다!')
# 3. 키워드 매개변수
# 함수 호출 시에 키워드를 붙여서 호출
# 매개변수의 순서를 지키지 않아도 된다.
def post_info(title, content):
print('제목 :', title)
print('내용 :', content)
post_info(content='없어요', title='여자친구 만드는 방법')
# 1. 위치 가변 매개변수
def print_fruits(*args): # 튜플형태로 받아옴
for arg in args:
print(arg)
print_fruits('apple','orange', 'mango', 'grape')
# 2. 키워드 가변 매개변수
def comment_info(**kwargs): # 딕셔너리 형태로 받아옴
for key, value in kwargs.items():
print(f'{key} : {value}')
comment_info(name='파린이', content='정말 감사합니다!')
1) 의미
2) 정의 방법
lambda 매개변수 : 결과
# 기본
lambda a: a-1
# if문 사용
lambda a: True if a>0 else False
3) 람다 함수 호출 방법
# 1.
lambda(a:a-1)(10)
# 2.
minus_one = lambda a : a-1
minus_one(10)
# 3.
lambda (a: True if a>0 else False)(-2)
1) map 함수 사용방법
map(함수, 순서가 있는 자료형)
# 순서가 있는 자료형 : list, tuple, dict, range
map(int, ['3','4','5','6']) # map object로 반환
# 리스트 모든 요소의 공백을 제거
items = [' 로지텍마우스 ', ' 앱솔키보드 ']
# 1) for 사용
for i in range(len(items)):
items[i] = items[i].strip()
print(items)
# 2) map 사용
def strip_all(x):
return x.strip()
items = list(map(strip_all, items))
# 3) 람다 함수 사용
items = list(map(lambda x:x.strip(), items))
print(items)
2) filter 함수 사용방법
filter(함수, 순서가 있는 자료형)
def func(x):
return x<0
filter(func,[-3,-2,0,5,7]) # filter object로 반환
# 리스트에서 길이가 3이하인 문자들만 필터링
animals = ['cat', 'tiger', 'dog', 'bird', 'monkey']
# for 사용
result = []
for i in animals:
if len(i) <= 3:
result.append(i)
print(result)
# filter 사용
def word_check(x):
return len(x) <= 3
result = list(filter(word_check, animals))
# 람다 함수 사용
result = list(filter(lambda x: len(x)<=3, animals))
print(result)
출처 : 패스트캠퍼스 Python & Django로 시작하는 웹 프로그래밍