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

Kursinnehåll

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.

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 6. Kapitel 11
Vi beklagar att något gick fel. Vad hände?
some-alt