Python lambda
Published Apr 23, 2022Updated Jul 11, 2022
The lambda keyword produces anonymous function expressions without using the def keyword. Lambda expressions are commonly used for defining single-use functions which are then passed to higher-order functions.
Syntax
lambda parameter_list : expression
A comma-separated parameter_list is provided and mapped into the expression on the other side of the colon :.
Example
A lambda expression can be assigned to a variable like in the example below:
example = lambda base, exponent : base**exponentprint(example(2, 3))
Codebyte Example
Below is another example of using a lambda expression:
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours