map,filter, lambda
map을 사용하여 리스트 속 값을 하나씩 대입
아래는 중복 코드
people = [
{'name': 'bob', 'age': 20},
{'name': 'carry', 'age': 38},
{'name': 'john', 'age': 7},
{'name': 'smith', 'age': 17},
{'name': 'ben', 'age': 27},
{'name': 'bobby', 'age': 57},
{'name': 'red', 'age': 32},
{'name': 'queen', 'age': 25}
]
map만 사용
def check_adult(person):
return ('성인' if person['age']>20 else '청소년')
#people을 하나하나 돌면서 check_adult에 넣어라
result = map(check_adult,people)
# 그 리턴값을 모아서 리스트로 만듬
print(list(result))
map & lambda 사용
# 함수 선언 없이 사용 가능
# 기본형 map(lamda x:x, people)
result = map(lambda person: ('성인' if person['age']>20 else '청소년'),people)
# 그 리턴값을 모아서 리스트로 만듬
print(list(result))
map & filter 사용
: filter은 좀 더 직관적인 편이다.
# filter는 좀더 직관적인 편이다.
result = filter(lambda person:person['age'] > 20, people)
함수의 매개변수
원래 함수는 매개변수에 써있는대로 인수를 받아온다
def cal(a,b):
return a+2*b
result = cal(1,2)
print(result)
def cal(a,b):
return a+2*b
result = cal(b=2,a=1)
print(result)
입력값의 개수를 지정하지않고 모두 받는 방법
def cal(*args):
for name in args:
print(f'{name} 밥무라~')
cal('영수','철수', '영희')
딕셔너리로 받을 수 있게 하는 방법
def cal(**kwargs):
print(kwargs)
cal(name='bob', age=30, height=180)
클래스
게임을 활용한 클래스 활용법
class Monster():
hp = 100
alive = True
def damage(self, attack):
self.hp = self.hp - attack
if self.hp < 0:
self.alive = False
def status_check(self):
if self.alive:
print('살았다')
else:
print('죽었다')
#m1 = 인스턴스
m1 = Monster()
m1.damage(150)
m1.status_check()
m2 = Monster()
m2.damage(90)
m2.status_check()
오늘의 하루
: 드디어 파이썬 기초 강의를 다 들었다. 아무리 정리하느라 오래 걸린다고 해도, 하루 해야할 양이 밀리고있어서 조급한 마음이 든다.
주말을 잘 활용하여 진도를 잘 따라 가야 할 것 같다...