Lambda Expression-
- 1) It is a keyword used to define a function as an expression.
- 2) All the functions can not be written as lambda expression.
- 4) If any function is having only 1 line logic statement then only that function can be written as Lambda expression.
- 5) We can also return the value from Lambda expression.
Ex-(Normal Function)-
def test(name):
print("Hi-",name)
name = input("Enter your name-")
test(name)
Output-
Enter your name-Python
Hi- Python
Converted Above Function In Lambda Expression-
name = input("Enter your name-")
test = lambda name: print("Hi-", name)
test(name)
Output-
Enter your name-Python
Hi- Python
Ex-(Normal Function)-
def even_chk(no):
if no%2 == 0:
return True
else:
return False
no = int(input("Enter the no-"))
if even_chk(no):
print("This is an even no.")
else:
print("This is not an even no.")
Output-
Enter the no-9
This is not an even no.
Converted Above Function In Lambda Expression-
even_chk = lambda n:n%2==0
no = int(input("Enter the No-"))
if even_chk(no):
print("This is an even no.")
else:
print("This is not an even no.")
Output-
Enter the No-8
This is an even no.
Ex-
biggest = lambda a,b,c: a if a>b and a>c else b if b>c else c
x = int(input("Enter 1st no-"))
y = int(input("Enter 2nd no-"))
z = int(input("Enter 3rd no-"))
print("Biggest no is-", biggest(x, y, z))
Output-
Enter 1st no-7
Enter 2nd no-4
Enter 3rd no-3
Biggest no is- 7