Home » Python

Python | Lambda function with Example

Python lambda function with example: Here, we are going to learn about lambda function and learn how to write one line code by using lambda?
Submitted by Pankaj Singh, on October 12, 2018

With the help of lambda function, we can create one line function definition.

Note: Function must have return type and parameter

Example:

Convert temperature from Celsius to Fahrenheit

1) Approach 1: Using normal way

# function definition to convert from c to F
def ctof(c):
    f=9/5*c+32
    return f


# input 
c=int(input("Enter Temp(C): "))
# function call
f=ctof(c)
# print 
print("Temp(F) :",f)

Output

Enter Temp(C): 10
Temp(F) : 50.0

2) Approach 2: Using lambda

# lambda function 
ctof = lambda c:9/5*c+32

# input 
c=int(input("Enter Temp(C):"))

# function call
f=ctof(c)

# print 
print("Temp(F) :",f)

Output

Enter Temp(C): 10
Temp(F) : 50.0




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.