Introduction of Python
Getting Started with Python
Syntax in Python
Comment in Python
Variables in Python
Data Types in Python
Numbers in Python
Castings in Python
Strings in Python
Boolean in Python
Operators in Python
List in Python
Tuples in Python
Sets in Python
Dictionary in Python
if Else Statement in Python
While Loop in Python
For Loop in Python
Functions in Python
Lambda Function in Python
Arrays in Python
Classes and Objects in Python
Inheritance in Python
Iterators in Python
Scope in Python
Modules in Python
Dates in Python
Math in Python
JSON in Python
RegEx in Python
Try and Except in Python
User Input in Python
String Formatting in Python
Lambda Function Overview
Python me Lambda Function ka use short lines of code ke liye hota hai. Lambda function me kai sare arguments diye ja sakte hain lekin expression ek hi use kar sakte hain aur yah single single of code se result bhi return kar deta hai.
Lambda Function ka Syntax:
lambda arguments : expression
Lambda Function Examples
#single argument
a= lambda x : x*3
print(a(4))
#two arguments
a = lambda x,y : x*y
print(a(2,10))
#more than two arguments
a = lambda x,y,z : x*y+z
print(a(2,10,20))
Lambda Function ka filter() ke sath use
filter() function kisi bhi input ko condition ke according perform karke output provide karta hai. Jaise agar hum number of sequence se odd numbers ko filter out kara chahte hain. Example me filter() lambda ko ek function aur “num” list ko ek argument ki tarah treat kar raha hai aur sath hi output bhi list ke format me de raha hai.
num = [34,54,63,32,21,45,67,89,88,90]
odd_num = list(filter(lambda x: (x%2 != 0) , num))
print(odd_num)
Lambda Function ka map() ke sath use
map() function list ke har ek element ko lambda function ke according modify karega aur apna output bhi list ke format me return karega.
num = [34,54,63,32,21,45,67,89,88,90]
odd_num = list(map(lambda x: (x*2) , num))
print(odd_num)