Python Lambda Function

Python Lambda Function Tutorial

Python Lambda Functions ( Anonymous function )

Anonymous functions in Python, also known as lambda functions, allow you to create small, one-line functions without explicitly defining a function using the `def` keyword.

Syntax: `lambda arguments: expression`

The `lambda` keyword is used to define a lambda function. It is followed by the arguments (comma-separated) and a colon (:), then the expression that is evaluated and returned as the function’s result. Here is an example:

				
					# Creating a lambda function to add two numbers
add_numbers = lambda x, y: x + y

# Calling the lambda function
result = add_numbers(10, 15)
print(result)  # Output: 25

				
			

In this example, we define a lambda function `add_numbers` that takes two arguments, `x` and `y`. The function adds these two numbers together using the `+` operator and returns the result. We then call the lambda function by providing the arguments `10` and `15`, and store the returned value in the `result` variable.

Lambda functions are commonly used when you need a simple, one-line function and don’t want to define a separate named function. They are often used in combination with built-in functions like `map()`, `filter()`, and `reduce()` to perform operations on iterables in a concise and readable manner.

Features of Python Lambda Functions:

  1. Anonymity: Lambda functions do not have a name, making them useful for short, simple operations where defining a separate function using def is not necessary.
  2. Compactness: Lambda functions are typically concise and can be defined in a single line.
  3. Single Expression: Lambda functions are limited to a single expression, which means they cannot contain multiple statements or complex logic.
  4. Functional Programming: Lambda functions are commonly used in functional programming to pass behavior as arguments to higher-order functions like map, filter, and reduce.

Difference between Built-in functions and Lambda functions (also known as Anonymous functions)

In Python, the main difference between built-in functions and anonymous functions (also known as lambda functions) lies in their structure, creation, and usage.

Built-in Functions:

Built-in functions in Python are pre-defined functions that are provided by the Python language itself. They are readily available for use without any additional coding or import statements. These functions cover a wide range of tasks and operations, such as mathematical calculations, string manipulations, list operations, file I/O, etc. They are named and can be used repeatedly throughout the code. Here’s an example of using a built-in function to find the length of a list:

				
					# Using the built-in function len() to find the length of a list
numbers = [1, 2, 3, 4, 5]
length = len(numbers)
print(length)  # Output: 5

				
			

In this example, the len() function is a built-in function that returns the number of elements in the list numbers.

Lambda Functions (Anonymous Functions):

Lambda functions are a special type of function in Python that allows you to create small, one-line functions without explicitly defining them using the def keyword. Lambda functions are typically used for simple, one-time tasks and are often used in conjunction with higher-order functions like map(), filter(), and reduce().

Lambda functions have the following structure: lambda arguments: expression

The lambda function takes a set of arguments, performs the specified expression on those arguments, and returns the result of the expression. Here’s an example of a lambda function that calculates the square of a number:

				
					# Using a lambda function to calculate the square of a number
square = lambda x: x**2
result = square(5)
print(result)  # Output: 25

				
			

In this example, we create a lambda function that takes an argument x, calculates the square using the expression x**2, and then we call the lambda function with square(5) to calculate the square of 5.

Key Differences:

  1. Structure and Definition: Built-in functions are predefined and come with Python, whereas lambda functions are created on-the-fly and are not explicitly defined using the def keyword.
  2. Usage and Complexity: Built-in functions are used for a wide range of tasks and can be complex with multiple parameters, whereas lambda functions are typically used for simple operations with one or few parameters.
  3. Naming: Built-in functions have names, and you can call them using their names whenever needed. Lambda functions, on the other hand, do not have names and are often used as temporary, inline functions.
  4. Return Statement: Built-in functions return their results as any other function, using the return statement. Lambda functions automatically return the value of the expression without using the return statement.
  5. Use Cases: Built-in functions are ideal for general-purpose and reusable tasks, while lambda functions are more suited for immediate, short-term, and one-time uses.

Leave a Comment