In this article Lambda Functions in Python we give the information about In Python, functions are reusable blocks of code that can be used repeatedly. Functions with arguments are those functions which can be passed data (arguments) while calling them.
Lambda Functions in Python:
How to create a function with arguments in Python?
To define a function with arguments in Python, you define the parameters inside parentheses (()).
Example of Function with Arguments
def greet(name): # ‘name’ is the parameter
print(f”Hello, {name}!”)
# Pass arguments while calling the function
greet(“Amit”)
greet(“Sonia”)
Output:
Hello, Amit!
Hello, Sonia!
Here:
- Name is the parameter, which is written while defining the function.
- “Amit” and “Sonia” are the arguments which are passed while calling the function.
Use of Multiple Arguments
In Python you can pass more than one argument to a function.
def add_numbers(a, b): # two parameters: a and b
print(f”Addition: {a + b}”)
add_numbers(5, 10)
add_numbers(15, 20)
Output:
Addition: 15
Addition: 35
Default Arguments
If you want to make an argument optional, you can assign a default value. If the caller does not pass that argument, the default value will be used.
def greet(name, message=”How are you?”): # Default value of ‘message’
print(f”Hello, {name}! {message}”)
greet(“Amit”) # Only ‘name’ passed
greet(“Sonia”, “What are you doing?”) # Both arguments passed
Output:
Hello, Amit! How are you?
Hello, Sonia! What are you doing?
Keyword Arguments
In Python you can also pass arguments along with their names. This is called keyword arguments. This makes the code more readable.
def introduce(name, age):
print(f”My name is {name} and my age is {age} years.”)
introduce(name=”Amit”, age=25) # Use of Keyword arguments
introduce(age=30, name=”Sonia”) # Will work even if the order is changed
Output:
My name is Amit and I am 25 years old.
My name is Sonia and I am 30 years old.
Arbitrary Arguments (*args)
If you want to pass a variable number of arguments to the function, you can use *args.
def print_numbers(*numbers): # ‘numbers’ will be a tuple
print(“Numbers:”, numbers)
print_numbers(1, 2, 3, 4, 5)
print_numbers(10, 20)
Output:
Numbers: (1, 2, 3, 4, 5)
Numbers: (10, 20)
Arbitrary Keyword Arguments (**kwargs)
If you want to pass a variable number of keyword arguments to the function, you can use **kwargs.
def display_info(**info): # ‘info’ will be a dictionary
for key, value in info.items():
print(f”{key}: {value}”)
display_info(name=”Amit”, age=25, city=”Delhi”)
Output:
name: amit
age: 25
city: Delhi
Summary
- Simple Argument:
Values are passed while calling the function.
- Default Argument:
The default value of the argument is defined.
- Keyword Argument:
Argument is passed along with name.
- *args:
To pass arbitrary number of positional arguments.
- **kwargs:
To pass arbitrary number of keyword arguments.
Lambda Functions in Python:
Lambda Functions in Python are a kind of small and anonymous functions. These are defined in one line and are useful for performing a specific task. Lambda Functions are used when we have to write a small function, which is to be used only once.
Structure of Lambda Function
Lambda functions are written using the lambda keyword. Its structure is as follows:
lambda arguments: expression
lambda: This is the keyword that represents Lambda Function.
Arguments: This contains the inputs that you pass to the function.
Expression: It contains the calculation or operation that the function will return.
Example 1: A Simple Lambda Function
# Addition of two numbers
add = lambda x, y: x + y
print(add(5, 10)) # Output: 15
Where are Lambda Functions used?
Lambda Functions are often used for small tasks, such as:
- To make expressions short and simple.
- With higher-order functions like map(), filter(), and reduce().
Example 2: Lambda Function with map()
# Find the square of each number
numbers = [1, 2, 3, 4]
squares = list(map(lambda x: x ** 2, numbers))
print(squares) # Output: [1, 4, 9, 16]
Example 3: Lambda Function with filter()
# Sort only even numbers
numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens) # Output: [2, 4, 6]
Example 4: Lambda Function with reduce()
from functools import reduce
# Multiply all numbers
numbers = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, numbers)
print(product) # Output: 24
Difference between Lambda Functions and Normal (Def) Functions
Lambda Function
There is only one expression.
There is no name.
Are called Anonymous functions.
Useful only for simple operations.
General (Def) Function
Supports multiple lines.
There is a name.
Are called normal or named functions.
Suitable for complex operations.
Lambda Functions are a good choice for small and concise operations, but when the code is complex, it is better to use a normal def function.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining