Skip to content

Python Lambda Functions

New Course Coming Soon:

Get Really Good at Git

Lambda functions (also called anonymous functions) are tiny functions that have no name and only have one expression as their body.

In Python they are defined using the lambda keyword:

lambda <arguments> : <expression>

The body must be a single expression. Expression, not a statement.

This difference is important. An expression returns a value, a statement does not.

The simplest example of a lambda function is a function that doubles that value of a number:

lambda num : num * 2

Lambda functions can accept more arguments:

lambda a, b : a * b

Lambda functions cannot be invoked directly, but you can assign them to variables:

multiply = lambda a, b : a * b

print(multiply(2, 2)) # 4

The utility of lambda functions comes when combined with other Python functionality, for example in combination with map(), filter() and reduce().

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my Python Handbook
→ Get my Python Handbook

Here is how can I help you: