0% completed
A lambda function in Python is a small, anonymous function that can have any number of arguments but only one expression. Unlike regular functions defined with def
, lambda functions are often used for short, simple operations. They are commonly used in cases where a function is needed temporarily.
lambda arguments: expression
lambda
keyword is used to define the function.add
takes two arguments (x
and y
).x + y
).add(5, 3)
outputs 8
.students
contains tuples with names and ages.sorted()
function sorts them based on age (x[1]
).Lambda functions are anonymous, single-expression functions used for simple operations. They are often used with built-in functions like map()
, filter()
, and sorted()
. While they make code concise, they should be used only when a short function is required, as regular functions (def
) are easier to read for complex logic.
.....
.....
.....