Contents

Python Interactive Guide - Step 3 Functions (7) - Lambda Expressions

Series - Python Interactive Guide
Info
  • 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.”

So far, we’ve learned about regular functions using the def keyword.
Python provides another way to define functions more concisely called 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.

Basic syntax of lambda expressions
lambda parameter1, parameter2, ...: expression
  • 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
📚 Exercise

Create a lambda expression that takes two numbers as input and returns their sum.

Example Solution
📚 Exercise

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

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.

📚 Exercise

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

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)

Lambda expressions are particularly useful in the following cases:

  1. Simple operations: When defining functions that perform simple operations expressible in one line
  2. Temporary use: When defining small functions used only once
  3. Concise code: When you want to write shorter code

Conversely, regular function definitions should be used in the following cases:

  1. Complex operations: When you need multiple statements or complex conditional logic
  2. Documentation: When you need to explain the function with docstrings
  3. Reusability: When the function will be used repeatedly in multiple places

In this section, we learned the following points about Python lambda expressions:

  1. Basics of Lambda Expressions: A concise syntax for creating anonymous functions
  2. Conditional Expressions in Lambda: Implementing conditional logic using ternary operators
  3. 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.

Related Content