[Link]@yahoo.
com Naresh i Technologies
Lambda Functions, Modules
& Packages
Lambda Function:
A lambda function is a function which has no name. Lambda function is also called
as an anonymous function.
Normal functions are defined with def keyword whereas lambda functions are
defined with lambda keyword.
The syntax of lambda function:
Return_value=lambda arguments : expression
Lambda function can have any number of arguments but only one expression. The
expression is evaluated and returned.
Example of lambda function:
x=lambda a : a*a*a
print(x(5))
In the above example lambda a : a*a*a is a lambda function, here a is an
arugment, a*a*a is an expression. That expression is evaluated and returned.
def vs lambda example:
def cube(a):
return a*a*a
b=cube(5)
print(b)
c=lambda a:a*a*a
d=c(5)
Python By Venkatesh Mansani Naresh i Technologies
[Link]@[Link] Naresh i Technologies
print(d)
Lambda functions are used in functional programming.
Functional programming means function can be passed as a parameter to
another function.
Example:
class Test:
def iseven(self, x):
return x%2==0
class Demo:
def operation(self, t, x):
l=[]
for i in x:
if [Link](i):
[Link](i)
return l
d=Demo()
t=Test()
y=[32,36,91,34,81]
l2=[Link](t,y)
print(l2)
There are three inbuilt functions to perform functional
programming operations.
1) filter() function
2) map() function
3) reduce() function
Python By Venkatesh Mansani Naresh i Technologies
[Link]@[Link] Naresh i Technologies
The filter() function:
The filter() function is used to create list containing of values for which the
function returns true.
Syntax:
filter(function, iterable_object)
This function can be used with user-defined functions as well as lambda functions
as a parameter.
Example:
x=[32,36,91,34,81]
def iseven(a):
return a%2==0
y=list(filter(iseven,x))
print(y)
z=list(filter(lambda i: i%2==0, x))
print(z)
The map() function:
The map() function takes another function as a parameter along with iterable
object and returns an output after applying the function to each iterable present
in the sequence.
Syntax:
map(function, iterable_object)
The map function can take user-defined functions as well as lambda functions as a
parameter.
Example:
x=[3,8,1,4,6]
Python By Venkatesh Mansani Naresh i Technologies
[Link]@[Link] Naresh i Technologies
def cube(a):
return a*a*a
y=list(map(cube,x))
print(y)
z=list(map(lambda i:i*i*i, x))
print(z)
The reduce() function:
The reduce() function takes a function with iterable object and returns a single
value.
Syntax:
reduce(function, iterables)
This function needs to be imported from the functools module.
Example:
import functools
x=[3,8,1,4,6]
def add(a,b):
return a+b
y=[Link](add,x)
print(y)
z=[Link](lambda a,b:a+b, x)
print(z)
Modules:
Python file itself is known as a module.
A module can contain global variables, statements, functions, classes, .. etc.,
Python By Venkatesh Mansani Naresh i Technologies
[Link]@[Link] Naresh i Technologies
The properties of one module can be accessed in another module by using import
statement.
Python supports two types of import statements:
1) Normal Import
2) From Import
1) Normal Import:
In normal import all properties of module are imported.
In this way, we can access properties of module by using module name only.
Example1:
[Link]
x=10
def cube(a):
print(a*a*a)
def max(a,b):
if a>b:
return a
else:
return b
[Link]
import test
print(test.x)
[Link](5)
c=[Link](20, 83)
print(c)
Python By Venkatesh Mansani Naresh i Technologies
[Link]@[Link] Naresh i Technologies
Example2:
In this normal import, while importing module, we can also create an alias name
for a module.
If we create an alias name for a module, then you can access properties by using
an alias name also.
import test as t
print(t.x)
[Link](5)
c=[Link](20, 83)
print(c)
2) From Import:
By using from import statement we can import only required properties also.
In this way, we can access properties of module without module name.
Example1:
[Link]
x=10
def cube(a):
print(a*a*a)
def max(a,b):
if a>b:
return a
else:
return b
[Link]
from test import x, cube, max
Python By Venkatesh Mansani Naresh i Technologies
[Link]@[Link] Naresh i Technologies
print(x)
cube(5)
c=max(20, 83)
print(c)
Example2:
In this approach also all properties can be imported at a time by using * symbol.
from test import *
print(x)
cube(5)
c=max(20, 83)
print(c)
Packages:
A package is collection of modules. Package can have sub packages also.
Package is equivalent to file or directory where as module is equivalent to file.
We can import modules of a package by using package_name.module_name.
We can also import modules of sub packages by using
package_name.sub_package_name.module_name.
Package Program Example:
C:\MyApp\pack>start notepad [Link]
def add(a, b):
print(a+b)
def sub(a, b):
return a-b
C:\MyApp>start notepad [Link]
Python By Venkatesh Mansani Naresh i Technologies
[Link]@[Link] Naresh i Technologies
import [Link]
[Link](10,20)
x=[Link](20, 10)
print(x)
In the above example MyApp is a normal folder, pack is a package name & Test is
a module name.
Test module present in a pack package.
By
Mr. Venkatesh Mansani
Naresh i Technologies
Python By Venkatesh Mansani Naresh i Technologies