Python Interactive Guide - Step 3 Functions (7) - Lambda Expressions
- This course, Python Interactive Guide, is designed to help you learn the basics of Python programming through hands-on, interactive examples.
- The “Style Guide” sections introduce clean coding practices, mainly based on PEP8.
- You can run and experiment with every code example.
Feel free to try things out - reloading the page will reset everything.
This is a continuation of “Step 3 Functions.”
3.6. Lambda Expressions
So far, we’ve learned about regular functions using the def
keyword.
Python provides another way to define functions more concisely called lambda expressions.
3.6.1. Basics of Lambda Expressions
Lambda expressions are a concise syntax for creating anonymous functions (functions without a name).
They are particularly useful when defining small functions that perform simple operations.
- After the
lambda
keyword, parameters are specified separated by commas (,
), followed by a colon (:
) and an expression - Lambda expressions can contain only a single expression (multiple statements or multi-line operations cannot be used)
- Lambda expressions return the result of evaluating the expression (no
return
statement is needed or allowed) - Lambda expressions can be used directly, but they are typically assigned to variables for use
Create a lambda expression that takes two numbers as input and returns their sum.
Example Solution
Create a lambda expression that takes an age as input and returns a boolean value indicating whether the person is an adult (18 or older).
Example Solution
3.6.2. Conditional Expressions in Lambda
Since lambda expressions can only contain a single expression, ternary operators are used for conditional logic:
In the grade
function above, we nested multiple ternary operators.
For such complex conditions, readability decreases, so it’s better to use regular functions instead.
Create a lambda expression that takes a string as an argument and returns “Long text” if the length is 10 or more characters, otherwise “Short text”.
(You can use the len
function to get the length of a string)
Example Solution
3.6.3. Comparison with Regular Functions
Lambda expressions differ from regular functions in the following ways:
Item | Lambda Expressions | Regular Functions |
---|---|---|
Syntax | Uses the lambda keyword |
Uses the def keyword |
Body | Single expression only | Can contain multiple statements |
Return Value | Automatically returns the evaluation result | Requires a return statement |
Name | None | Has a name |
Docstring | Cannot have one | Can have one |
In other respects, lambda expressions share the same characteristics as regular functions:
-
Default arguments can be specified
-
Variable-length arguments (
*args
parameter,**kwargs
parameter) can be used -
External variables are referenced at execution time, not definition time (When external variables are referenced)
When to Use Lambda Expressions
Lambda expressions are particularly useful in the following cases:
- Simple operations: When defining functions that perform simple operations expressible in one line
- Temporary use: When defining small functions used only once
- Concise code: When you want to write shorter code
Conversely, regular function definitions should be used in the following cases:
- Complex operations: When you need multiple statements or complex conditional logic
- Documentation: When you need to explain the function with docstrings
- Reusability: When the function will be used repeatedly in multiple places
Summary
In this section, we learned the following points about Python lambda expressions:
- Basics of Lambda Expressions: A concise syntax for creating anonymous functions
- Conditional Expressions in Lambda: Implementing conditional logic using ternary operators
- Comparison with Regular Functions: Differences and when to use each
Lambda expressions are especially useful for creating simple, temporary functions.
However, for complex operations or highly reusable functions, regular function definitions are more appropriate.
In the next section, we’ll learn about higher-order functions, which are often used with lambda expressions and can take functions as arguments or return them as results.