Course Name: Python for Trading: Basic, Section No: 6, Unit No: 7, Unit type: Notebook
In the part related to Lambda operator the examples are for the function definition not usage of Lambda operator. Can be this corrected then ?
Course Name: Python for Trading: Basic, Section No: 6, Unit No: 7, Unit type: Notebook
In the part related to Lambda operator the examples are for the function definition not usage of Lambda operator. Can be this corrected then ?
Hi Piotr,
Thanks for the feedback!
We’ll work on making the section clearer with a simpler explanation of the difference between defining functions using def
and the lambda
operator, and when to use each one based on the need.
Here’s a quick explanation to help you:
In Python, both def
and lambda
are used to define functions, but they serve different purposes:
def
is used to define regular named functions, usually when the function contains multiple lines or more complex logic.lambda
is used to create small, one-line anonymous functions (functions without a name), typically used for short tasks like in map()
or filter()
as is also explained in the unit.Try this example—both versions do the same thing, just written differently:
def add_df(x, y):
return x + y
add_lambda = lambda x, y: x + y # named version of lambda for comparison with def
print(add_df(5, 3))
print(add_lambda(5, 3))
I hope it helps.