本文共 732 字,大约阅读时间需要 2 分钟。
With the help of lambda function, we can create one line function definition.
借助lambda函数,我们可以创建一个行函数定义。
Note: Function must have return type and parameter
注意:函数必须具有返回类型和参数
Example:
例:
Convert temperature from Celsius to Fahrenheit
将温度从摄氏温度转换为华氏温度
1) Approach 1: Using normal way
1)方法1:使用常规方法
# function definition to convert from c to Fdef ctof(c): f=9/5*c+32 return f# input c=int(input("Enter Temp(C): "))# function callf=ctof(c)# print print("Temp(F) :",f)
Output
输出量
Enter Temp(C): 10Temp(F) : 50.0
2) Approach 2: Using lambda
2)方法2:使用lambda
# lambda function ctof = lambda c:9/5*c+32# input c=int(input("Enter Temp(C):"))# function callf=ctof(c)# print print("Temp(F) :",f)
Output
输出量
Enter Temp(C): 10Temp(F) : 50.0
翻译自:
转载地址:http://uttzd.baihongyu.com/