can you do lambda in python

In Python, a lambda or anonymous function is a small, nameless function that can take any number of arguments, but can only have one expression. The syntax for lambda function is as follows:

main.py
lambda arguments: expression
29 chars
2 lines

Here's an example to illustrate how lambda functions can be used:

main.py
# normal function to add two numbers
def addition(a, b):
    return a + b

# equivalent lambda function
addition_lambda = lambda a, b: a + b

# calling the functions
print(addition(2, 3)) # Output: 5
print(addition_lambda(2, 3)) # Output: 5
241 chars
11 lines

In the example above, we defined two functions addition and addition_lambda that both take two arguments and return the sum of them. We can see that the lambda function is more concise and easier to define.

gistlibby LogSnag