If coding tutorials with math examples are the bane of your existence, keep reading. This series uses relatable examples like dogs and cats.
Lambda functions are unnamed functions. This means it is not defined like a normal function.
Defining lambdas is similar to defining a regular function, but it has a different syntax.
# syntax of a function def name(args): # syntax of lambda function lambda args: expression # can only have one expression lambda args, args, args: expression # may have multiple parameters
Note: Lambdas can only handle expressions. Expressions must have
return values like numbers,
# A regular function def add_two_numbers(a, b): return a + b # Same function written as a lambda function add_two_numbers = lambda a, b: a + b
Notice, both take args
b, then add them together. The only difference is that the first one is defined as a function.
For my math loathing pals
# A regular function def generate_full_name(first_name, last_name): return first_name + ' ' + last_name # Same function written as a lambda function generate_full_name = lambda first_name, last_name: first_name + ' ' + last_name
Lambda functions can be used inside of other functions.
# future home of an example # if you could help me with a simple example that would be badass # a non-math example would be appreciated forever # simple math would be cool too (something like 2+2)
If all of this makes sense to you and you'd like a little more fanciness and excitement, lookup 'Self invoking lambda function'
Note: Lambdas are not something I'm really comfortable with. For more details check out this thorough article about Lambdas in Python.
Series based on