Open In App

Difference between Normal def defined function and Lambda

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss the difference between normal def defined function and lambda in Python.

Def keyword​​​​​​​

In python, def defined functions are commonly used because of their simplicity. The def defined functions do not return anything if not explicitly returned whereas the lambda function does return an object. The def functions must be declared in the namespace. The def functions can perform any python task including multiple conditions, nested conditions or loops of any level, printing, importing libraries, raising Exceptions, etc. 

Example:

Python3




# Define function to calculate cube root
# using def keyword
  
def calculate_cube_root(x):
    return x**(1/3)
  
# Call the def function to calculate cube
# root and print it
print(calculate_cube_root(27))
  
# Define function to check if language is present in
# language list using def keyword
languages = ['Sanskrut', 'English', 'French', 'German']
  
def check_language(x):
    if x in languages:
        return True
    return False
  
# Call the def function to check if keyword 'English'
# is present in the languages list and print it
print(check_language('English'))


Output:

3.0
True

Lambda keyword

The lambda functions can be used without any declaration in the namespace. The lambda functions defined above are like single-line functions. These functions do not have parenthesis like the def defined functions but instead, take parameters after the lambda keyword as shown above. There is no return keyword defined explicitly because the lambda function does return an object by default.

Example:

Python3




# Define function using lambda for cube root
cube_root= lambda x: x**(1/3)
  
# Call the lambda function
print(cube_root(27))
  
  
languages = ['Sanskrut', 'English', 'French', 'German']
  
  
# Define function using lambda
l_check_language = lambda x: True if x in languages else False
  
# Call the lambda function
print(l_check_language('Sanskrut'))


Output:

3.0
True

Table of Difference Between def and Lambda

def defined functions

lambda functions

Easy to interpret

Interpretation might be tricky

Can consists of any number of execution statements inside the function definition

The limited operation can be performed using lambda functions

To return an object from the function, return should be explicitly defined

No need of using the return statement

Execution time is relatively slower for the same operation performed using lambda functions

Execution time of the program is fast for the same operation

Defined using the keyword def and holds a function name in the local namespace

Defined using the keyword lambda and does not compulsorily hold a function name in the local namespace



Previous Article
Next Article

Similar Reads

Python def Keyword
Python def keyword is used to define a function, it is placed before a function name that is provided by the user to create a user-defined function. In Python, a function is a logical unit of code containing a sequence of statements indented under a name given using the “def” keyword. In Python def keyword is the most used keyword. Python def Synta
7 min read
Why do Python lambda defined in a loop with different values all return the same result?
In this article, we are going to learn why Python lambda defined in a loop with different values all return the same result. In this article, we will explore the reasons behind this behavior and discuss how to avoid it. We will start by looking at how lambda functions are defined and executed, and how they capture the values of variables that are u
6 min read
Difference between Generator and Normal Function
Normal functions in Python are used for traditional computation tasks, with execution proceeding from start to finish, typically returning a single result. On the other hand, generator functions employ the `yield` statement to produce values lazily, preserving their state across multiple calls. This allows generators to efficiently handle large dat
4 min read
Difference between List comprehension and Lambda in Python
List comprehension is an elegant way to define and create a list in Python. We can create lists just like mathematical statements and in one line only. The syntax of list comprehension is easier to grasp. A list comprehension generally consists of these parts : Output expression,Input sequence,A variable representing a member of the input sequence
3 min read
Python - Lambda function to find the smaller value between two elements
The lambda function is an anonymous function. It can have any number of arguments but it can only have one expression. Syntax lambda arguments : expression In this article, we will learn how to find the smaller value between two elements using the Lambda function. Example: Input : 2 5 Output : 2 Input : 7 5 Output : 5Method 1: Using lambda and min(
2 min read
Map function and Lambda expression in Python to replace characters
Given a string S, c1 and c2. Replace character c1 with c2 and c2 with c1. Examples: Input : str = 'grrksfoegrrks' c1 = e, c2 = r Output : geeksforgeeks Input : str = 'ratul' c1 = t, c2 = h Output : rahul We have an existing solution for this problem in C++. Please refer to Replace a character c1 with c2 and c2 with c1 in a string S. We can solve th
2 min read
Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space. Examples: Input : [1, 2, 3, 2, 3, 1, 3] Output : 3 We have existing solution for this problem please refer Find the Number Occurring Odd Number of Times link. we will solv
1 min read
Intersection of two arrays in Python ( Lambda expression and filter function )
Given two arrays, find their intersection. Examples: Input: arr1[] = [1, 3, 4, 5, 7] arr2[] = [2, 3, 5, 6] Output: Intersection : [3, 5] We have existing solution for this problem please refer Intersection of two arrays link. We will solve this problem quickly in python using Lambda expression and filter() function. Implementation: C/C++ Code # Fun
1 min read
Ways to sort list of dictionaries by values in Python - Using lambda function
In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge to sort dictionaries according to their values can
2 min read
Nested Lambda Function in Python
Prerequisites: Python lambda In Python, anonymous function means that a function is without a name. As we already know the def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions. When we use lambda function inside another lambda function then it is called Nested Lambda Function. Example 1: #
2 min read
Practice Tags :