Open In App
Related Articles

Difference between Normal def defined function and Lambda

Improve Article
Save Article
Like Article

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. 



# 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
# 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



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.



# Define function using lambda for cube root
cube_root= lambda x: x**(1/3)
# Call the lambda function
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



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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 19 Dec, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials