Functions can be created using lambda syntax. These functions are known ass anonymous functions. This approach is most commonly used when passing a simple function as an argument to another function.
#named function def simple_func(x): return x*2 + 5-x + 4 print(simple_func(-3)) #lambda function print((lambda x: x*2 + 5-x + 4) (-3))
The terminal output will be:
ddn_ro@linux:~/Desktop$ python file.py 6 6 ddn_ro@linux:~/Desktop$
A Lambda definition does not include a “return” statement, it always contains an expression which is returned. You can put a lambda definition anywhere a function is expected, and you don’t have to assign it to a variable at all.
Lambda functions aren’t as powerful as named functions.
They can only do things that require a single expression – usually equivalent to a single line of code.
# This is a normal function def f (x): return x**2 print f(8) # This is a lambda function g = lambda x: x**2 print g(8)
The output in terminal will be:
ddn_ro@linux:~/Desktop$ python file.py 64 64 ddn_ro@linux:~/Desktop$