0% found this document useful (0 votes)
47 views8 pages

Lambda Functions in Python Basics

The document discusses lambda functions, modules, and packages in Python. It defines lambda functions as anonymous functions defined using the lambda keyword. Modules allow organizing Python code into files and namespaces, and can be imported and used between files. Packages are collections of modules that allow further organizing related modules. Key points include: - Lambda functions have no name and are defined with lambda arguments: expression syntax. - Modules are Python files that contain code that can be imported and accessed between files using the import statement. - Packages are collections of modules that allow grouping related code and importing modules from sub-packages.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views8 pages

Lambda Functions in Python Basics

The document discusses lambda functions, modules, and packages in Python. It defines lambda functions as anonymous functions defined using the lambda keyword. Modules allow organizing Python code into files and namespaces, and can be imported and used between files. Packages are collections of modules that allow further organizing related modules. Key points include: - Lambda functions have no name and are defined with lambda arguments: expression syntax. - Modules are Python files that contain code that can be imported and accessed between files using the import statement. - Packages are collections of modules that allow grouping related code and importing modules from sub-packages.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[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

Common questions

Powered by AI

A package in Python is a collection of modules and possible subpackages, whereas a module is essentially a single Python file. A package is represented by a directory that contains a special file named '__init__.py', which can sometimes be empty. To import a module from a package, you use the syntax 'import package_name.module_name'. For example, if 'Test' is a module within the 'pack' package, you import it with 'import pack.Test', and then access its functions and variables using the 'pack.Test' prefix .

In Python, you can create an alias for a module using the 'as' keyword with the import statement. For example, 'import test as t' creates an alias 't' for the 'test' module. This can be useful for shortening long module names, avoiding naming conflicts if the original module name clashes with another variable name, or simply for convenience and readability in the code. It allows for a more streamlined workflow when the module is frequently used .

Lambda functions can be used with the 'filter' function in Python to create lists containing values for which a given condition is true. The 'filter' function takes two arguments: a function and an iterable object. Lambda functions are often used here to write quick, concise conditional tests inline without explicitly defining a separate named function. For example, to filter even numbers from a list, one could use: 'list(filter(lambda i: i%2==0, x))', where 'x' is the iterable .

The 'map' function in Python applies a given function to each item of an iterable (e.g., list) and returns a list of the results. It can work with both user-defined and lambda functions. For instance, using a lambda function to cube each number in a list, we can write: 'z = list(map(lambda i: i*i*i, x))'. Here, 'map' applies the lambda function, which calculates the cube of each element, to the list 'x'. 'x' can be any iterable, such as a list of numbers .

Lambda functions in Python offer several advantages, such as simplicity and conciseness for small or one-off functions, which are particularly useful in contexts requiring functional programming like 'map', 'filter', and 'reduce'. They improve code readability when the function's utility is limited or named functions would be unnecessarily verbose. However, disadvantages include limited functionality, as they can only contain a single expression and cannot have statements or annotations. Additionally, their anonymous nature can obscure what the code does if overused in complex scenarios .

The 'filter' function in Python uses a function to test each element of an iterable (e.g., a list) and returns a new iterable with only the elements that pass the test. For instance, if you want to filter out even numbers, you can use a user-defined function such as: 'def iseven(a): return a % 2 == 0'. Apply this function with 'filter': 'y = list(filter(iseven, x))', where 'x' is the input list. This will return a list of elements from 'x' that are even .

The 'from import *' statement imports all objects from a module into the current namespace, allowing access to the module's functions and variables directly by their names. This can make the code cleaner and simpler to write when many elements are needed. However, it may lead to a cluttered namespace, increasing the risk of name collisions and reducing code readability and maintainability because it is unclear which module an object originates from. It's generally recommended to use specific imports to avoid these issues .

The 'reduce' function in Python processes an iterable to produce a single cumulative result, using a given binary function. It takes two arguments: a function and an iterable. Unlike 'map', which applies a function to each element independently and returns a list of results, 'reduce' applies the function cumulatively to the items of an iterable, so as to reduce the iterable to a single value. For example, reducing a list to its sum can be achieved by using: 'functools.reduce(lambda a, b: a + b, x)'. 'reduce' must be imported from the functools module .

A lambda function in Python is an anonymous function, meaning it does not have a name. It is defined using the 'lambda' keyword rather than the 'def' keyword used in normal functions. A lambda function can have any number of arguments but only one expression, which is evaluated and returned. Unlike regular functions, lambda functions are limited to a single line of code. They are often used when a simple function is needed for a short period and are a core part of functional programming in Python .

Python provides two main ways to import modules into a script: 'Normal Import' and 'From Import'. With 'Normal Import', a module is included in its entirety using the 'import module_name' syntax, and its functions and variables are accessed using 'module_name.property'. An alias can also be created using 'import module_name as alias'. 'From Import' allows specific elements of a module to be imported directly using 'from module_name import property', enabling access to properties without the module name and potentially all elements using an asterisk (*).

You might also like