Python Intermediate and Advanced

Python Intermediate_009: Lambda Functions in Python

codeaddict 2025. 4. 5. 21:26

Lambda functions, also known as anonymous functions, are a concise way to create small, throwaway functions in Python. They are often used for short, simple operations that are not reused elsewhere in the code. In this tutorial, we’ll cover the syntax of lambda functions, compare them with regular functions, provide examples, and solve problems using both approaches.

 

1. Syntax of Lambda Functions

The syntax for a lambda function is:

lambda arguments: expression
  • lambda: The keyword used to define a lambda function.
  • arguments: The input parameters (similar to a regular function).
  • expression: A single expression that is evaluated and returned. Lambda functions can only contain one expression.

2. Examples: Lambda vs Regular Functions

Example 1: Adding Two Numbers

Lambda Function:

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

Regular Function:

def add(x, y):
    return x + y
print(add(3, 5))  # Output: 8

Explanation:

  • The lambda function is a one-liner and doesn’t require a def statement or a function name.
  • The regular function is more readable and reusable for larger tasks.

Example 2: Multiplying Two Numbers

Lambda Function:

multiply = lambda x, y: x * y
print(multiply(4, 5))  # Output: 20

Regular Function:

def multiply(x, y):
    return x * y

print(multiply(4, 5))  # Output: 20

Example 3: Checking if a Number is Even

Lambda Function:

is_even = lambda x: x % 2 == 0
print(is_even(4))  # Output: True
print(is_even(5))  # Output: False

Regular Function:

def is_even(x):
    return x % 2 == 0
print(is_even(4))  # Output: True
print(is_even(5))  # Output: False

Example 4: Concatenating Strings

Lambda Function:

concat = lambda s1, s2: s1 + " " + s2
print(concat("Hello", "World"))  # Output: Hello World

Regular Function:

def concat(s1, s2):
    return s1 + " " + s2
print(concat("Hello", "World"))  # Output: Hello World

3. Problems and Solutions

Problem 1: Square a Number

Task: Write a program to square a number.

Solution with Lambda:

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

Solution with Regular Function:

def square(x):
    return x**2

print(square(5))  # Output: 25

Problem 2: Check if a Number is Positive

Task: Write a program to check if a number is positive.

Solution with Lambda:

is_positive = lambda x: x > 0
print(is_positive(10))  # Output: True
print(is_positive(-5))  # Output: False

Solution with Regular Function:

def is_positive(x):
    return x > 0
print(is_positive(10))  # Output: True
print(is_positive(-5))  # Output: False

Problem 3: Calculate the Area of a Rectangle

Task: Write a program to calculate the area of a rectangle.

Solution with Lambda:

area = lambda length, width: length * width
print(area(10, 5))  # Output: 50

Solution with Regular Function:

def area(length, width):
    return length * width

print(area(10, 5))  # Output: 50

4. When to Use Lambda vs Regular Functions

Use Lambda When:

  • The function is short and simple.
  • The function is used only once (throwaway).
  • You need a quick function for simple operations.

Use Regular Functions When:

  • The function is complex or has multiple statements.
  • The function is reused multiple times.
  • You need better readability and documentation.