</> Code Editor { } Code Formatter

Python Lambda Functions Exercises


1/18

Python Lambda Functions Practice Questions

Correct
0%

Which of the following defines a lambda function that returns the square of a number?


This exercise tests understanding of basic lambda syntax in Python.

  • Option 1: Correct. lambda x: x**2 defines an anonymous function that takes one argument x and returns its square. This is a proper lambda expression.
  • Option 2: Incorrect. lambda x: print(x**2) prints the square instead of returning it.
  • Option 3: Incorrect. While def square(x): return x**2 works, it is a normal function, not a lambda.
  • Option 4: Incorrect. lambda x: x*2 multiplies the number by 2, not square.

Example usage:

square = lambda x: x**2
print(square(5))  # 25
print(square(10)) # 100

Tip: Lambda functions are anonymous, single-expression functions useful for small calculations. They automatically return the result of the expression.

Quick Recap of Python Lambda Functions Concepts

If you are not clear on the concepts of Lambda Functions, you can quickly review them here before practicing the exercises. This recap highlights the essential points and logic to help you solve problems confidently.

Python Lambda Functions — Overview

Lambda functions in Python are small, anonymous functions created using the lambda keyword. They are useful for quick, one-off operations without defining a full function with def.

Key points:

  • Ideal for short, simple computations
  • Can be assigned to variables or used directly
  • Always return the result of a single expression

Example:

square = lambda x: x ** 2
print(square(7))  # Output: 49

print((lambda x, y: x + y)(5, 3))  # Output: 8

Key Characteristics of Python Lambda Functions

FeatureDescription
AnonymousCan be used without a name; optionally assign to a variable
Single ExpressionOnly one expression is allowed, no multiple statements
Implicit ReturnReturns the value of the expression automatically
CompactReduces boilerplate for small functions

Example of a simple lambda function demonstrating compactness:

multiply = lambda a, b: a * b
print(multiply(4, 6))  # Output: 24

Python Lambda Function Syntax

The general syntax of a lambda function is:

lambda arguments: expression

Explanation:

  • arguments – comma-separated input parameters
  • expression – single operation whose result is returned automatically
  • Note: Lambda functions cannot include loops, multiple statements, or explicit return

Example of basic lambda usage:

square = lambda x: x ** 2
print(square(7))  # Output: 49

add = lambda x, y: x + y
print(add(5, 3))  # Output: 8

Lambda Functions with Multiple Arguments

Lambda functions can accept more than one argument. Simply separate the arguments with commas.

# Lambda with two arguments
multiply = lambda a, b: a * b
print(multiply(4, 6))  # Output: 24

# Lambda with three arguments
maximum = lambda x, y, z: max(x, y, z)
print(maximum(12, 7, 19))  # Output: 19

Tip: Use descriptive variable names for clarity.

Nested Lambda Functions

You can create a lambda function that returns another lambda. This is useful for creating simple closures or higher-order functions.

# Lambda returning another lambda
power = lambda n: (lambda x: x ** n)
cube = power(3)
print(cube(5))  # Output: 125

# Another nested example
adder = lambda a: (lambda b: a + b)
add_five = adder(5)
print(add_five(10))  # Output: 15

Using Lambda for Conditional Expressions

Lambda functions can include a single-line if-else expression for quick conditional computations.

# Lambda with a conditional expression
sign = lambda x: "Positive" if x > 0 else "Non-positive"
print(sign(8))   # Output: Positive
print(sign(-3))  # Output: Non-positive

Assigning and Reusing Lambda Functions

You can assign a lambda function to a variable to reuse it like a normal function.

# Assigning lambda to a variable
area = lambda length, width: length * width
print(area(5, 10))  # Output: 50
print(area(7, 3))   # Output: 21

Practical Tips

Keep lambda functions simple and readable. For more complex logic, use regular functions.

# Keep lambda functions simple
add = lambda x, y: x + y

# For complex logic, use a regular function
def complex_operation(a, b):
    if a > b:
        return a - b
    return b - a
  • Avoid overcomplicating lambdas; they are meant for simple tasks.
  • Use meaningful variable names for readability.
  • Combine with other Python features for practical use (closures, sorting, etc.).

Summary: Key Points About Python Lambda Functions

  • Lambda functions are anonymous, single-expression functions in Python.
  • They automatically return the result of the expression.
  • Can accept multiple arguments and even return other lambdas.
  • Best for short, one-off tasks; avoid complex logic inside lambdas.
  • Improves code brevity while keeping it readable.
  • Understanding lambdas is essential for writing clean, functional-style Python code.


About This Exercise: Python – Lambda Functions

Welcome to Solviyo’s Python – Lambda Functions exercises, a practical collection designed to help learners master anonymous functions in Python. In this section, we focus on the core ideas behind lambda expressions, their syntax, usage with built-in functions like map, filter, and reduce, and real-world applications. These exercises come with clear explanations and answers so you can learn confidently and understand every concept step by step.

What You Will Learn

Through these exercises, you will explore how lambda functions simplify Python code and make it more readable, including:

  • Understanding what lambda functions are and how they differ from regular Python functions.
  • Writing concise, one-line functions using lambda expressions for quick computations.
  • Applying lambda functions in practical scenarios such as sorting, conditional expressions, and functional programming patterns.
  • Recognizing when to use lambda functions and when a named function might be more readable.
  • Exploring scope, arguments, and return values within lambda functions with hands-on exercises, explanations, and answers.

These exercises are designed to be approachable but practical, helping you understand not just how lambda functions work, but why they are a useful tool in Python programming. A Quick Recap section is also available for refreshing key concepts before practicing.

Why Learning Lambda Functions Matters

Lambda functions are widely used in Python, especially in data processing, functional programming, and coding interviews. Mastering them helps you write cleaner, more compact code and improves your ability to read complex Python scripts. By practicing with our exercises, MCQs, explanations, and answers, you will strengthen your understanding of anonymous functions, avoid common mistakes, and build confidence in using Python for real-world tasks and projects.

Start Strengthening Your Python Skills

With Solviyo’s Lambda Functions exercises, you can start practicing immediately with hands-on tasks and MCQs. Each question comes with explanations and answers so you can verify your learning as you go. Regular practice will help you grasp lambda functions thoroughly, making your Python code more concise, readable, and professional-ready. Dive in and boost your Python skills step by step!