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
Difference between Normal def defined function and Lambda
In this article, we will discuss the difference between normal def defined function and lambda in Python.