python Tutorial 8 | lambda
前言
什麼事lambda
程式
加法
x = lambda a : a + 10
print(x(5))def add2(a):
x = a + 10return xprint(add2(5))
乘法
x = lambda a, b : a * b
print(x(5, 6))def multiply2(a, b):
x = a * breturn xprint(multiply2(5, 6))
比大小
x = lambda c, d : c if c > d else d
print(x(4, 5))def compare2(c, d):
x = 0
if c > d:
x = celse :
x = d
return xprint(compare2(5,4))
BMI值
def BMI_calculator(height, weight):
height = float(height)
BMI = 0.0 #歸零
height = height/100 #公分換算成公尺
BMI = weight / (height**2)#體重(公斤)除以(身高的平方)return BMIprint(BMI_calculator(162,45))x = lambda height, weight : weight / ((height/100)**2)
print(x(162.0, 45.1))