Lambda Functions, Modules & Packages
Lambda Functions, Modules & Packages
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 t.iseven(i):
l.append(i)
return l
d=Demo()
t=Test()
y=[32,36,91,34,81]
l2=d.operation(t,y)
print(l2)
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)
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]
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)
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=functools.reduce(add,x)
print(y)
z=functools.reduce(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.,
The properties of one module can be accessed in another module by using import
statement.
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:
test.py
x=10
def cube(a):
print(a*a*a)
def max(a,b):
if a>b:
return a
else:
return b
demo.py
import test
print(test.x)
test.cube(5)
c=test.max(20, 83)
print(c)
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)
t.cube(5)
c=t.max(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:
test.py
x=10
def cube(a):
print(a*a*a)
def max(a,b):
if a>b:
return a
else:
return b
demo.py
from test import x, cube, max
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.
import pack.Test
pack.Test.add(10,20)
x=pack.Test.sub(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