Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Lambda Functions in Python | Functions in Python
Introduction to Python
course content

Course Content

Introduction to Python

Introduction to Python

1. First Acquaintance with Python
2. Variables and Types in Python
3. Conditional Statements in Python
4. Other Data Types in Python
5. Loops in Python
6. Functions in Python

book
Lambda Functions in Python

All the functions you've created so far are stored in memory after running the code for the first time. However, sometimes you may not want to define a separate function—especially for simple, straightforward tasks. In these cases, Python's lambda function can be useful, as it creates anonymous functions.

python

Let's illustrate this by revisiting our earlier function. You can rewrite it using a lambda function to return the squared sum of two numbers

1234
# Define lambda function sq = lambda x, y: (x + y)**2 # Test it print('Sum of 2 and 3 squared is', sq(2, 3))
copy

Note

Not all functions discussed can be converted into lambda functions. Typically, lambda functions are best suited for concise operations that fit within a single line.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 6. Chapter 11
We're sorry to hear that something went wrong. What happened?
some-alt