Types of functions:
Lambda Functions:
These are small, one-line functions that are defined using the lambda keyword.
They are also called "anonymous" because they are typically not assigned to a name.
Useful for short operations.
Advantage of anonymous function:
Concise coding.
Better performance.
Syntax of a Lambda Function:
A lambda function has the following syntax
lambda arguments: expression
lambda is the keyword used to define the function.
arguments is a comma-separated list of parameters.
expression is a single expression that the lambda function evaluates and returns.
Convert function into lambda function:
1.Remove def
2.Remove function name
3.Remove return
4.Add lambda
Example for function:
def add(no1,no2):
return no1+no2
result=add(100,200)
print(result)
Example for lambda function:
result = lambda no1,no2 : no1+no2
print(result(100,200))
Output:
300
Example for function:
def find_bigger(no1,no2):
if no1>no2:
return no1
else:
return no2
print(find_bigger(10,5))
Example for lambda function:
bigger = lambda no1,no2: no1 if no1>no2 else no2
print(bigger(10,5))
Output:
10
Higher order functions:
In programming, a higher-order function is a function that:
Takes one or more functions as arguments, or
Returns a function as a result.
1.filter():
The filter() function in Python is used to filter out elements from an iterable (such as a list, tuple, or string) based on a condition specified by a function.
Syntax of filter()
filter(function, sequence)
function:
A function that tests whether each element of the iterable should be included. This function should return True or False.
sequence:
The sequence(e.g., list, tuple, etc.) that is being filtered.
Example for filter():
def check(no):
if no%5==0:
return True
return False
l = [10,20,2,3,4,5,67,8,9,15,25,32,33,34,35]
print(list(filter(check, l)))
Example for filter() using lambda function:
l = [10,20,2,3,4,5,67,8,9,15,25,32,33,34,35]
lambda no:no%5==0
print(list(filter(lambda no:no%5==0, l)))
Output:
[10, 20, 5, 15, 25, 35]
2.map():
The map() function returns a map object, which is an iterator. You often need to convert it into a list, tuple, or another collection to see the results.
Syntax of map()
map(function, sequence)
function:
A function that takes one or more arguments and returns a value. This function will be applied to each item of the iterable.
sequence:
An sequence (e.g., a list, tuple, etc.) whose elements will be processed by the function.
Example for map():
l = [10,20,30,40,50]
def find_power(no):
return no**2
print(list(map(find_power, l)))
Example for map() using lambda function():
l = [10,20,30,40,50]
print(list(map(lambda no : no**2, l)))
Output:
[100, 400, 900, 1600, 2500]
3.reduce():
The reduce() function in Python is a part of the functools module and is used to apply a binary function cumulatively to the items of an iterable, from left to right, so as to reduce the iterable to a single value.
Syntax:
from functools import *
reduce(function, sequence)
function:
A function that takes two arguments. It will be applied cumulatively to the items of the iterable.
sequence:
The sequence (like a list, tuple, etc.) whose items will be reduced.
Example for reduce():
from functools import *
l = [10,20,30,40]
print(reduce(lambda total, no:total+no , l))
Output:
100
Function aliasing:
The alias (the new variable) can be used in place of the original function, and any changes made to the function will be reflected in both the original and aliased variables because they point to the same function in memory.
Example:
def greet(name):
print('Welcome', name)
greet('Lakshmi')
display = greet
print(id(greet))
print(id(display))
display('Pritha')
Output:
Welcome Lakshmi
23283606721008
23283606721008
Welcome Pritha
Decorator function:
A Function which takes a function as argument and extends its functionality, returns modified result.
A decorator in Python is a function that takes another function as input and extends or alters its behavior without modifying its actual code.Python provides a simpler syntax for applying decorators, which uses the @ symbol.
Example:
def decor(func1):
def inner(name):
if name == 'Guido':
print('Oh! Unexpected Guest, Welcome Mr.', name)
else:
func1(name)
return inner
@decor #Decorator Function
def greet(name):
print('Welcome', name)
greet('Guru')
greet('Muthu')
greet('Pritha')
greet('Guido')
Output:
Welcome Guru
Welcome Muthu
Welcome Pritha
Oh! Unexpected Guest, Welcome Mr. Guido
Example for two decorator function:
def decor1(func1):
def inner():
no = func1()
return no + 5
return inner
def decor(func1):
def inner():
no = func1()
return no ** 2
return inner
@decor1
@decor #Decorator Function
def greet():
return 100
print(greet())
Output:
10005
Generator function:
A generator function in Python is a function that returns an iterator and yields values one at a time using the yield keyword.
Example 1:
def generator_demo():
yield 100
yield 200
yield 'ABCD'
result = generator_demo()
print(type(result))
print(next(result))
print(next(result))
print(next(result))
Output:
<class 'generator'>
100
200
ABCD
Example 2:
def display_first_n_numbers(num):
no = 1
while no<=num:
yield no
no+=1
result = display_first_n_numbers(5)
print(type(result))
for number in result:
print(number)
Output:
<class 'generator'>
1
2
3
4
5
Top comments (0)