0% found this document useful (0 votes)
10 views5 pages

Python Session 16 Lambda Functions

Uploaded by

ameen1ahmad06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Python Session 16 Lambda Functions

Uploaded by

ameen1ahmad06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lambda function

lambda function also a functions but in a single line

Like how we represented list in list comprehensions

Case-1: function with one argument

In [5]: def add(a):


return(a+10)
add(10)

Out[5]: 20

function name : add

function argument : a

return output : a+10

function_name = lambda arg : return_output

In [12]: add= lambda a : a+10


add(10)

Out[12]: 20

In [14]: def cube(x):


return(x**3)
cube(10)

Out[14]: 1000

In [16]: cube= lambda x : x**3


cube(10)

Out[16]: 1000

Case-2

In [19]: def add(a,b):


return(a+b)
add(10,20)

Out[19]: 30

In [21]: add= lambda a,b : a+b


add(10,20)

Out[21]: 30
In [23]: def add(a,b=100):
return(a+b)
add(200)

Out[23]: 300

In [25]: add= lambda a,b=100 : a+b


add(10)

Out[25]: 110

case-3: if

In [28]: def even(a):


if a%2==0:
return('even')
even(100)

Out[28]: 'even'

In [ ]: # [<if_output> <forloop> <if_con>]


# [<ifout> <if con> else <else>]
# fun_name= lambda <arg> : <if_out> <if cond>

only if will not work

In [34]: even= lambda a : 'even' if a%2==0 else 'odd'


even(27)

Out[34]: 'odd'

In [ ]: [output forloop]
[<output> for loop <if cond>]
[<if_out> <if_con> else <else_out> <for loop>]
[<if_out> <if_con> else
<elif_out> <elif_con (if) else>
<else_out> for loop]

In [37]: def gretaer(a,b):


if a>b:
return(f"{a} is greater")
else:
return(f"{b} is greater")
gretaer(100,200)

Out[37]: '200 is greater'

In [39]: greater= lambda a,b : f"{a} is greater" if a>b else f"{b} is greater"
greater(200,100)

Out[39]: '200 is greater'

Case-4: How to apply iterators

In [44]: # Q1) input=['hyd','bengaluru','mumbai','chennai']


# output=['Hyd','Bengaluru','Mumbai','Chennai']
# using for loop call each element apply capitalize then apppend in a outi

# Q2) input=['hyd','bengaluru','mumbai','chennai']
# output=['HYD','BENGALURU','MUMBAI','CHENNAI']

Input=['hyd','bengaluru','mumbai','chennai']
[i.upper() for i in Input]

Out[44]: ['HYD', 'BENGALURU', 'MUMBAI', 'CHENNAI']

In [46]: # step-1: make your function


#func_name= lambda arg : output

lambda i: i.upper()

Out[46]: <function __main__.<lambda>(i)>

In [50]: # step-2: add a list


# func_name= lambda arg : output,list
Input=['hyd','bengaluru','mumbai','chennai']
lambda i : i.upper(),Input # a,b

#<function __main__.<lambda>(i)>,['hyd','bengaluru','mumbai','chennai']

Out[50]: (<function __main__.<lambda>(i)>, ['hyd', 'bengaluru', 'mumbai', 'chennai'])

map

In [57]: #lambda i: i.upper()


# lambda i: i.upper(),Input
# map(lambda i: i.upper(),Input)
# zip(a,b) #

map(lambda i : i.upper(),Input)

Out[57]: <map at 0x2c661f5d690>

In [59]: list(map(lambda i : i.upper(),Input))

Out[59]: ['HYD', 'BENGALURU', 'MUMBAI', 'CHENNAI']

In [ ]: lambda i: i.upper()
lambda i: i.upper(),Input
map(lambda i: i.upper(),Input)
list(map(lambda i : i.upper(),Input))

In [ ]: # Q1) input=['hyd','bengaluru','mumbai','chennai']
# output=['Hyd','Bengaluru','Mumbai','Chennai']
# list(map(lambda i : <output>,Input))
# Q2) Input= [10,20,30,-10,-20,-30]
# output = [1,1,1,0,0,0]
# list(map(lambda i : <output>,Input))

In [61]: Input=['hyd','bengaluru','mumbai','chennai']
list(map(lambda i : i.capitalize(),Input))

Out[61]: ['Hyd', 'Bengaluru', 'Mumbai', 'Chennai']


In [63]: Input= [10,20,30,-10,-20,-30]
list(map(lambda i : 1 if i>0 else 0,Input))

Out[63]: [1, 1, 1, 0, 0, 0]

In [65]: # Q3) input=['hyd','bengaluru','mum#bai','chen#nai']


# output=['mum#bai','chen#nai']

list(map(lambda i : i if '#' in i,Input))

Cell In[65], line 4


list(map(lambda i : i if '#' in i,Input))
^
SyntaxError: expected 'else' after 'if' expression

map will use for if any transformation

in that transformation we can use if -else also

but we can not use only if

filter

filter is used to extract the outputs if condition match

here in output place we will write only condition with out if

In [ ]: # Q3) input=['hyd','bengaluru','mum#bai','chen#nai']
# output=['mum#bai','chen#nai']
# '#' in i

lambda arg : cond


lambda arg : cond , list1
filter (lambda arg: cond,list1)
list(filter(lambda arg: cond,list1))

In [83]: Input=['hyd','bengaluru','mum#bai','chen#nai']
list(filter(lambda i : '#' in i,Input))

Out[83]: ['mum#bai', 'chen#nai']

map : transformation can use if-else

filter: extraction only condition

sum of list

In [87]: l=[10,20,30,40,50]
sum(l)

Out[87]: 150
In [89]: summ=0
for i in l:
summ=summ+i
print(summ)

150

reduce

In [92]: from functools import reduce

In [ ]: map(func,list1) : lambda arg:output


filter(func,list1) : lambda arg:cond
reduce(func,list1) : lambda arg:output

In [96]: l=[10,20,30,40,50]
reduce(lambda a,b: a+b,l)

Out[96]: 150

In [98]: l=[1,2,3,4]
reduce(lambda a,b: a*b,l)

Out[98]: 24

In [100… l=[10,2,30,4]
reduce(lambda a,b: a if a>b else b,l)

Out[100… 30

In [ ]: a if a>b else b # comprision of only two value


a*b # multiply of only two value
a+b # addition of two apply

lambda reduce list ===

You might also like