Login
Congrats in choosing to up-skill for your bright career! Please share correct details.
Home / Blog / Data Science / Lambda Function in Python
Bharani Kumar Depuru is a well known IT personality from Hyderabad. He is the Founder and Director of AiSPRY and 360DigiTMG. Bharani Kumar is an IIT and ISB alumni with more than 17 years of experience, he held prominent positions in the IT elites like HSBC, ITC Infotech, Infosys, and Deloitte. He is a prevalent IT consultant specializing in Industrial Revolution 4.0 implementation, Data Analytics practice setup, Artificial Intelligence, Big Data Analytics, Industrial IoT, Business Intelligence and Business Management. Bharani Kumar is also the chief trainer at 360DigiTMG with more than Ten years of experience and has been making the IT transition journey easy for his students. 360DigiTMG is at the forefront of delivering quality education, thereby bridging the gap between academia and industry.
Table of Content
Alonzo Church introduced the lambda function to the field of mathematics in the 1930s. Python has an anonymous function called a lambda that is used to define functions without a reputation. In contrast to regular functions, lambda functions evaluate a function, accept any number of arguments, and return a single expression.
The lambda function has no name, in contrast to the 'def' function. It is a very straightforward Python function that just requires the lambda keyword, parameters, and expressions in a single line of code.
variable = lambda parameters_list: expression
The following operations can be carried out with the help of the lambda function;
Some advance operations include
add = lambda a, b: a + b print(add(5, 5)) # output: 10
Explanation: A lambda object add is defined first. The lambda expression is then initialised with the two arguments a and b. These two parameters are numbers that will be added together. The definition of the addition phrase follows the colon.
subtract = lambda a, b: a - b print(add(200, 50)) # output: 150
Explanation: A lambda object subtract is declared first. The lambda expression is then initialised with the two arguments a and b. The subtraction operation is to be carried out on these two parameters, which are integers. The definition of the subtraction expression follows the colon.
multiply = lambda a, b: a * b print(multiply(100, 50)) # output: 5000
Explanation: A lambda object multiply is declared first. The lambda expression is then initialised with the two arguments a and b. These two parameters are numbers that will be multiplied together. After the colon, we define the multiplication expression.
div = lambda a, b: a / b print(div(100, 50)) # output: 2
Explanation: A lambda object div is declared first. The lambda expression is then initialised with the two arguments a and b. These two parameters are numbers that will be used to divide. The definition of the division phrase follows the colon.
Lambda expressions with conditional statements are a very useful technique and also the lines of code can be reduced. In this, the lambdas can hold only one expression at a time
What is the syntax when we use lambda with if-else conditions in Python:
variable_name = lambda parameters: code_for_if if (condition) else code_for_else
In this syntax we define the expression before the if statement and then write the if statement with the condition, and the else block is defined after the if condition.
conditional_lambda = lambda x: x/100 if x < 20 else x print(conditional_lambda(5)) # output: 0.05
Explanation:
Example 1: Check whether the given number is an even or odd using the lambda function
Example 2: Check whether the given number is divisible or not by another number by getting two inputs from the user using the lambda function.
We can reuse the lambda function to add one number to the existing numbers. Let’s look into a simple example,
A. We will now explore how to use lambda functions on a single column using dataframe.assign() method.
B. We will now explore how to use lambda functions on multiple columns using dataframe.assign() method.
C. We will now explore how to use lambda functions on a single row using dataframe.apply() method.
D. We will now explore how to filter the data with lambda functions using filter() method.
Click here to learn more about python Can I Learn Python in One Month
E. We will now explore how to map the data with lambda functions using map() method.
Agra, Ahmedabad, Amritsar, Anand, Anantapur, Bangalore, Bhopal, Bhubaneswar, Chengalpattu, Chennai, Cochin, Dehradun, Malaysia, Dombivli, Durgapur, Ernakulam, Erode, Gandhinagar, Ghaziabad, Gorakhpur, Gwalior, Hebbal, Hyderabad, Jabalpur, Jalandhar, Jammu, Jamshedpur, Jodhpur, Khammam, Kolhapur, Kothrud, Ludhiana, Madurai, Meerut, Mohali, Moradabad, Noida, Pimpri, Pondicherry, Pune, Rajkot, Ranchi, Rohtak, Roorkee, Rourkela, Shimla, Shimoga, Siliguri, Srinagar, Thane, Thiruvananthapuram, Tiruchchirappalli, Trichur, Udaipur, Yelahanka, Andhra Pradesh, Anna Nagar, Bhilai, Borivali, Calicut, Chandigarh, Chromepet, Coimbatore, Dilsukhnagar, ECIL, Faridabad, Greater Warangal, Guduvanchery, Guntur, Gurgaon, Guwahati, Hoodi, Indore, Jaipur, Kalaburagi, Kanpur, Kharadi, Kochi, Kolkata, Kompally, Lucknow, Mangalore, Mumbai, Mysore, Nagpur, Nashik, Navi Mumbai, Patna, Porur, Raipur, Salem, Surat, Thoraipakkam, Trichy, Uppal, Vadodara, Varanasi, Vijayawada, Visakhapatnam, Tirunelveli, Aurangabad
ECIL, Jaipur, Pune, Gurgaon, Salem, Surat, Agra, Ahmedabad, Amritsar, Anand, Anantapur, Andhra Pradesh, Anna Nagar, Aurangabad, Bhilai, Bhopal, Bhubaneswar, Borivali, Calicut, Cochin, Chengalpattu , Dehradun, Dombivli, Durgapur, Ernakulam, Erode, Gandhinagar, Ghaziabad, Gorakhpur, Guduvanchery, Gwalior, Hebbal, Hoodi , Indore, Jabalpur, Jaipur, Jalandhar, Jammu, Jamshedpur, Jodhpur, Kanpur, Khammam, Kochi, Kolhapur, Kolkata, Kothrud, Ludhiana, Madurai, Mangalore, Meerut, Mohali, Moradabad, Pimpri, Pondicherry, Porur, Rajkot, Ranchi, Rohtak, Roorkee, Rourkela, Shimla, Shimoga, Siliguri, Srinagar, Thoraipakkam , Tiruchirappalli, Tirunelveli, Trichur, Trichy, Udaipur, Vijayawada, Vizag, Warangal, Chennai, Coimbatore, Delhi, Dilsukhnagar, Hyderabad, Kalyan, Nagpur, Noida, Thane, Thiruvananthapuram, Uppal, Kompally, Bangalore, Chandigarh, Chromepet, Faridabad, Guntur, Guwahati, Kharadi, Lucknow, Mumbai, Mysore, Nashik, Navi Mumbai, Patna, Pune, Raipur, Vadodara, Varanasi, Yelahanka
360DigiTMG - Data Science Course, Data Scientist Course Training in Chennai
D.No: C1, No.3, 3rd Floor, State Highway 49A, 330, Rajiv Gandhi Salai, NJK Avenue, Thoraipakkam, Tamil Nadu 600097
1800-212-654-321
Didn’t receive OTP? Resend
Let's Connect! Please share your details here