top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Python Concepts For Data Science: lambda expressions



Lambda expression is an anonymous function, it can take any numbers of arguments but can only have one expression returned.

Syntax: lambda arguments :

expression ---> the expression is executed and the result is returned in one line

Example01:

#example 01
x=lambda i:i+10
print(x(5))

We start by assigning lambda to a value (x here) so that everytime we need it we call x . Here we gave 5 as an argument for x. The result here will be : 15.


Example02: lambda with 2 arguments

#example 02 with 2 arguments
calculate= lambda x,y: x*y
print(calculate(5,9))

The result here will be 45.


Example03: lambda with 03 arguments

#example03 with 03 arguments
math_op = lambda x,y,z:x*y+z
print(math_op(2,3,4))

The result here will be 10.


The power of lambda is better shown when we use it as an anonymous function in another function


#example04with user function
def multiply (n):
    return lambda x:x*n
double=multiply(2)
print(double(5))

So we created a user function called multiply that has 1 argument n. Inside the body of the function we used a lambda function with a single argument x and it will return x*m

This lambda expression result is just a returning expression for the return statement in the multiply function.


How we call the function then?

first we assign it to a variable with an argument n : double = multiply(2) than we print this variable with the x argument. Here it will return 10.

Thank you for reading.

You will find the code below here:
















 
 
 

Comments


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page