+ 1

Can python lambda contain the statement?

Simply speaking notation of f = lambda : x * x for x in range(10). I have read that python is not a strict language, so that we can include additional if- statement or for clause there. You can refer to Deitel Python book which cover data science and AI for higher version.

4th Nov 2024, 2:33 AM
Oliver Pasaribu
Oliver Pasaribu - avatar
3 Respuestas
+ 4
your lambda cannot be parsed perhaps you mean f = lambda n: (x*x for x in range(n)) # f returns a generator expression, which can be unpacked by * print(*f(5))
4th Nov 2024, 3:47 AM
Bob_Li
Bob_Li - avatar
0
You can include as many statement in a single line in python if you know the way
4th Nov 2024, 5:44 PM
RuntimeTerror
RuntimeTerror - avatar
0
RuntimeTerror You are limited to one expression. True, you can make it long and complicated with multiple if else ir by nesting other lambdas inside, but they all still have to be in one long expression and you often end up with code that is hard to read. Nice for flexing, but not on production code It's better to use a regular functions for complicated cases.
5th Nov 2024, 2:23 AM
Bob_Li
Bob_Li - avatar