파이썬 람다(Lambda) 예제

 

다른 분 글 중에서 예제만 간단히 정리했다.

 

f = lambda x: x + 2

f(2)

 

출력

4

 

 

f = lambda x,y: x + y

f(1,2)

 

출력

3

 

(lambda x,y: x + y)(1,2)

 

출력

3

 

초를 분이나 시간으로 환산하는 예제

 

def sec2other(unit):

return lambda sec: sec/unit

 

sec2min = sec2other(60)

sec2hour = sec2other(3600)

 

print(sec2min(180))

print(sec2hour(7200))

 

 

환율 예제

 

def other2won(unit):

return lambda price: price*unit

 

usd2won = other2won(1160.50) #달러 환율

eur2won = other2won(1293.90) #유로 환율

 

print(usd2won(10)) #10달러는 한국돈 얼마?

print(eur2won(15)) #15유로는 한국돈 얼마?


<이상>

반응형

+ Recent posts