19-Lambda Function, Modules, CSV Files, Numpy-18-01-2023
19-Lambda Function, Modules, CSV Files, Numpy-18-01-2023
19-Lambda Function, Modules, CSV Files, Numpy-18-01-2023
Any file that contains a python code with a .py extension, we will call it a
module. For example, the samplemodule.py file is a module, and you can
use it in any python script by importing a module
with samplemodule name using an import statement.
Create a Module
In python, you can create a module by writing the following code in
the samplemodule.py file.
Import Module
In python, you can import the module by using the import keyword. To use the
module (samplemodule) that we created in any python script or interpreter, you need
to use an import statement like as shown below.
import samplemodule
Following is the example of using the module ( samplemodule) that we created in
python script.
import samplemodule
a = samplemodule.add(10, 40)
b = samplemodule.subtract(100, 50)
print("Sum:", a)
print("Subtract:", b)
for user in samplemodule.users:
print(user)
If you observe the above code, we imported the module ( samplemodule) and
accessing the functions and variables.
The above python module example will return the result as shown below.
Sum: 50
Subtract: 50
Suresh
Rohini
Trishika
If the defined module is not found, it will throw the ModuleNotFoundError exception.
First, it will search the current working directory path for the specified
module.
If not found, it will search the directories that are in
the PYTHONPATH environment variable.
If still not found, it will search the default installation directory.
Whenever the interpreter starts, the sys module will get all the location
paths and keep them in the list like as shown below.
import sys
print(sys.path)
The above python module example will return the result as shown below.
a = sm.add(10, 40)
b = sm.subtract(100, 50)
print("Sum:", a)
print("Subtract:", b)
for user in sm.users:
print(user)
In the above example, we created alias name (sm) for the imported
module (samplemodule) using as keyword and accessing the module
resources using the alias name.
The above python module example will return the result as shown below.
Sum: 50
Subtract: 50
Suresh
Rohini
Trishika
When you use from keyword to import specific module resources, you don’t need to
use the module name to refer to the module elements.
a = add(10, 40)
b = subtract(100, 50)
print("Sum:", a)
print("Subtract:", b)
In the above example, we imported only add function
from samplemodule using from and import keywords but, we are trying to access
both add and subtract functions.
The above python module example will return the result as shown below.
a = add(10, 40)
b = subtract(100, 50)
print("Sum:", a)
print("Subtract:", b)
The above python module example will return the result as shown below.
Sum: 50
Subtract: 50
import samplemodule as sm
x = dir(sm)
print(x)
The above python module example will return the result as shown below.
To get the latest module changes without closing the current interpreter session, you
need to use the reload() function from the imp module to reload the defined
modules.
import imp
imp.reload(samplemodule)
Built-in Modules
By default, python has provided a set of built-in modules such as math, os, sys, etc.,
to use in any python script file. The built-in modules will load automatically whenever
the interpreter starts, and these modules are available as a part of python libraries.
Python has a set of built-in modules such as math, os, sys, etc., to use in any
python script file. The built-in modules will load automatically whenever
the interpreter starts, and these modules are available as a part of python libraries.
The built-in modules that are available in python interpreter written in C. To see the
list of all available built-in modules, execute the help('modules') command in the
python console as shown below.
>>> help('modules')
The above command will return all the available modules in the python interpreter.
If the information is more than one page, it will display -- More -- as shown in the
image, if you want to see more info, press Enter.
You can also use the built-in dir() function to get the particular module's available
names and attributes.
>>> dir(math)
The above statement will return the result as shown below.
We will learn more about built-in modules in the next chapters
Math Constants
The following table lists the constants available in the python math module.
Constant Description
E val: 2.718281828459045
inf val: inf
Neg inf val: -inf
Method Description
math.comb() It returns the number of ways to choose k items from n items without
Method Description
math.copysign() It returns a float with the magnitude (absolute value) of x but the sign of y.
math.dist() Returns the Euclidean distance between two points (p and q).
math.isfinite() Returns True, if the number is neither an infinite nor a NAN, otherwise
returns False.
math.isinf() Returns True, if the number is positive or negative infinity, otherwise returns
False.
math.isnan() Returns True, if the number is NaN (not a number), otherwise False.
math.isqrt() It rounds the square root number downwards to the nearest integer
math.perm() Returns the number of ways to choose k items from n items without
repetition and with an order.
math.prod() Returns the product of all the elements in the input iterable.
math.remainder( It returns the closest value that can make the numerator completely divisible
) by the denominator
Math Functions
Same as constants, the math module has different methods to perform
mathematical operations.
8.0
6
5
125.0
-0.3048106211022167
-0.9524129804151563
0.320040389379563
The following table lists the different methods available in the math module to
perform logarithmic, trigonometric, etc., operations based on our requirements.
import random
Like the random() method, the random module has different methods to generate
random numbers based on your requirements.
Randint() Method
If you want to generate the random integer values between the defined range of
values, you need to use randint() method.
import random
Randrange() Method
Same as the randint() method, the randrange() method also will generate the
random value between the specified range, but it will accept three parameters
(start, stop, step).
The first parameter is to define the starting position and its optional. By default, it
will consider 0 as the starting position. The second parameter to define the stop
position and its required. The third parameter to specify the incrementation and its
optional. By default, it's 1.
import random
print(random.randint(2, 10))
print(random.randrange(2,20,5))
print(random.randrange(2,100,20))
The above random module program will generate the random numbers between the
defined range values using the randrange() method.
2
17
82
Seed() Method
If you want to generate the same random number when you execute the above
program, you need to use the seed(n) method in the random module. Here, the
value n should always be the same; if you want to change the random value, then
change the n value.
Choice() Method
The random module choice() method is useful to return the randomly selected
element from the specified sequence. The given sequence must be non-empty.
Method Description
choices() It will return a list with random elements from the specified
sequence.
sample() It will return a list with the defined number of random elements
from the sequence.
uniform() It will return a random float number between two given numbers.
triangular() It will return a random float number between two given numbers,
you can also use a mode parameter to specify the midpoint
between the two numbers.
betavariate() It will return a random number between 0.0 and 1.0 based on the
beta distribution.
import random
print(random.choice('Tutlane'))
print(random.choice([1, 3, 10, 40, 50, 60]))
print(random.choice((1, 3, 10, 40, 50, 60)))
The above random module example will return the result as shown below.
n
40
1
The following table lists the different methods available in the random module to
randomly generate the numbers, randomly pick the elements, etc., based on our
requirements.
import statistics
The above statistics module example will return the result as shown below.
20
7.5
Like the mean() method, the statistics module has different methods to calculate
mathematical statistics of numeric data.
Median() Method
The statistics module median() method is useful to calculate the median (middle)
value of the numeric data in the list.
Following is the example of using the statistics module median() method in python.
import statistics
20
7.5
Mode() Method
The statistics module mode() method is useful to return the most common data
point on the list.
Following is an example of using the mode() function to get the common data point
from the python list.
import statistics
10
5
tut
Stdev() Method
The statistics module stdev() method is useful to calculate the standard deviation of
the given data.
import statistics
9.082951062292475
2.5495097567963922
Variance() Method
The statistics module variance() method is useful to calculate the variance from a
given sample data.
import statistics
82.5
6.5
The following table lists the different statistics module methods to calculate the
mathematical statistics of numeric data based on our requirements.
Method Description
median_high() It will calculate the high median value of the numeric data
in the list.
median_low() It will calculate the low median value of the numeric data
in the list.
mode() It will return the most common data point in the list.
Python Packages
Modules are the files that will contain the python statements, classes, functions, etc.
A package is a combination of one or more module files in a single directory.
Using packages, you can manage a large number of python files in different folders
and subfolders based on your requirements.
In python, if you create a directory with python files and an empty __init__.py file
will be treated as a package. To consider any directory as a package, that directory
must contain the __init__.py file. By default, the file __init__.py is empty, but you
can write your package initialization code based on your requirements.
The __init__.py file can be empty, or it can contain some valid python code. The
code whatever you write in the __init__.py file will execute whenever the package is
imported. So, it's better to write your package initialization code, e.g., import other
modules or set some values. Here, we added abc.py and xyz.py modules files inside
the directory with the following code.
abc.py
def add(a, b):
return a + b
xyz.py
def multiply(a, b):
return a * b
Following is the sample way of accessing the modules from packages in python.
Addition: 30
Subtract: 10
Multiply: 200
Division: 5.0
Following is another way to access the modules from packages in python.
import Sample_Package.abc
import Sample_Package.xyz
Addition: 30
Multiply: 200
As discussed, every package will contain an empty __init__.py file. If required, you
can write your package initialization code in the __init__.py file to execute while
importing the package.
We will write a code in the __init__.py file to import the functions directly from the
packages instead of modules. Open your __init__.py file and write the code as
shown below.
__init__.py
from .abc import add
from .xyz import multiply
To test, create the testpkg.py file in the same directory where package
(Sample_Package) is located and write the code as shown below.
from Sample_Package import add, multiply
The above python packages example will return the result as shown below.
Addition: 30
Multiply: 200
Module Description
string module provides a set of functions that are used to perform certain
operations on characters.
string
This module has pre-defined functions like capwords, ascii_letters, etc.
Output:
200False10
FACEPrep
Output:
3.3333333333333335
3.333333333333333481363069950020872056484222412109375
Output:
18
16
12.908184799437432
Output:
FACE Prep
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
programming.
Such a program has a main routine through which smaller independent modules
(functions) are called upon. Each When called, a function performs a specified task
and returns the control back to the calling routine, optionally along with result of its
process.
Python interpreter has a number of built-in functions. They are always available for
use in every interpreter session. Many of them have been discussed in previously.
For example print() and input() for I/O, number conversion functions (int(), float(),
complex()), data type conversions (list(), tuple(), set()) etc. Here is complete list of
Built-in Functions
available as a part of libraries bundled with Python distributions. However they are
not available for use automatically. These functions are defined in modules. A
Most of the times, built-in modules are written in C and integrated with Python
interpreter.
A built-in module may be a Python script (with .py extension) containing useful
utilities.
To display list of all available modules, use following command in Python console:
>>> help('modules')
Resources from other modules are loaded by import statement. The general format
import module
module.function([arguments if any])
>>> math.sqrt(100)
10.0
os module
random module
math module
time module
sys module
collections module
statistics module
os module
mkdir():
>>> import os
>>> os.mkdir("d:\\tempdir")
created.
chdir():
>>> import os
>>> os.chdir("d:\\temp")
getcwd():
>>> os.getcwd()
'd:\\temp'
Directory paths can also be relative. If current directory is set to D drive and then to
temp without mentioning preceding path, then also current working directory will be
changed to d:\temp
>>> os.chdir("d:\\")
>>> os.getcwd()
'd:\\'
>>> os.chdir("temp")
>>> os.getcwd()
'd:\\temp'
In order to set current directory to parent directory use ".." as the argument to
chdir() function.
>>> os.chdir("d:\\temp")
>>> os.getcwd()
'd:\\temp'
>>> os.chdir("..")
>>> os.getcwd()
'd:\\'
rmdir():
The rmdir() function in os module removes a specified directory either with absolute
or relative path. However it should not be the current working directory and it should
be empty.
>>> os.chdir("tempdir")
>>> os.getcwd()
'd:\\tempdir'
>>> os.rmdir("d:\\temp")
>>> os.chdir("..")
>>> os.rmdir("temp")
listdir():
The os module has listdir() function which returns list of all files in specified
directory.
>>> os.listdir("c:\\Users")
random module
Python’s standard library contains random module which defines various functions
Mersenne Twister algorithm that produces 53-bit precision floats. Functions in this
>>> random.random()
0.755173688207591
>>> random.randint(1,100)
58
>>> random.randint(1,100)
91
random.randrange(): Returns a random element from the range created by start,
stop and step arguments. The start , stop and step parameters behave similar to
range() function.
>>> random.randrange(1,10)
>>> random.randrange(1,10,2)
>>> random.randrange(0,101,10)
40
>>> random.choice('computer')
'o'
>>> random.choice([12,23,45,67,65,43])
65
>>> random.choice((12,23,45,67,65,43))
23
>>> numbers=[12,23,45,67,65,43]
>>> random.shuffle(numbers)
>>> numbers
>>> numbers
math module
trigonometric functions
representation functions
logarithmic functions
Pie π which is defined as ratio of circumference to diameter of a circle and its value
>>> math.pi
3.141592653589793
>>> math.e
2.718281828459045
This module contains functions for calculating various trigonometric ratios for a
given angle. The functions (sin, cos, tan etc.) need angle in radians as argument. We
on the other hand are used to express angle in degrees. The math module presents
two angle conversion functions (degrees() and radians()) to convert angle in degrees
degrees)
>>> math.radians(30)
0.5235987755982988
>>> math.degrees(math.pi/6)
29.999999999999996
Following statements show sin, cos and tan ratios for angle of 30 degrees
(0.5235987755982988 radians)
>> math.sin(0.5235987755982988)
0.49999999999999994
>>> math.cos(0.5235987755982988)
0.8660254037844387
>>> math.tan(0.5235987755982988)
0.5773502691896257
sin(30)
0.499999999999999
0.5
94
0.866025403784438
cos(30) 3/2
7)
0.
tan(30) 1/2
5773502691896257
>>> math.log10(10)
1.0
math.exp(): returns a float number after raising e (math.e) to given number. exp(x)
is equivalent to e**x
>>> math.log10(10)
1.0
>>> math.e**10
22026.465794806703
math.pow(): This function receives two float arguments, raises first to second and
>>> math.pow(4,4)
256.0
>>> 4**4
256
>>> math.sqrt(100)
10.0
>>> math.sqrt(3)
1.7320508075688772
Representation functions:
The ceil() function approximates given number to smallest integer greater than or
equal to given floating point number. The floor() function returns a largest integer
>>> math.ceil(4.5867)
>>> math.floor(4.5687)
sys module
This module provides functions and variables used to manipulate different parts of
sys.argv
This return list of command line arguments passed to a Python script. Item at 0th
index of this list is always the name of the script. Rest of the arguments are stored at
subsequent indices.
Here is a Python script (test.py) consuming two arguments from command line.
import sys
This causes program to end and return to either Python console or command
sys.maxsize
>>> sys.maxsize
9223372036854775807
sys.path
This is an environment variable that returns search path for all Python modules.
>>> sys.path
These are file objects used by the interpreter for standard input, output and errors.
stdin is used for all interactive input (Python shell). stdout is used for the output of
print() and of input(). The interpreter’s prompts and error messages go to stderr.
sys.version
interpreter.
collections module
This module provides alternatives to built-in container data types such as list, tuple
and dict.
namedtuple() function
This function is a factory function that returns object of a tuple subclass with named
fields. Any valid Python identifier may be used for a field name except for names
collections.namedtuple(typename, field-list)
The typename parameter is the subclass of tuple. Its object has attributes mentioned
in field list. These field attributes can be accessed by lookup as well as by its index.
Following statement declares a employee namedtuple having name, age and salary
as fields
>>> e1.name
'Ravi'
Or by index
>>> e1[0]
'Ravi'
OrderedDict() function
same. The key-value pairs in normal dictionary object appear in arbitrary order.
>>> d1={}
>>> d1['A']=20
>>> d1['B']=30
>>> d1['C']=40
>>> d1['D']=50
print (k,v)
A 20
B 30
D 50
C 40
>>> d2=collections.OrderedDict()
>>> d2['A']=20
>>> d2['B']=30
>>> d2['C']=40
>>> d2['D']=50
print (k,v)
A 20
B 30
C 40
D 50
deque() function
A deque object supports append and pop operation from both ends of a list. It is
more memory efficient than a normal list object because in a normal list, removing
one of iem causes all items to its right to be shifted towards left. Hence it is very
slow.
>>> q=collections.deque([10,20,30,40])
>>> q.appendleft(110)
>>> q
>>> q.append(41)
>>> q
>>> q.pop()
40
>>> q
>>> q.popleft()
110
>>> q
statistics module
>>> statistics.mean([2,5,6,9])
5.5
median() : returns middle value of numeric data in a list. For odd items in list, it
returns value at (n+1)/2 position. For even values, average of values at n/2 and
>>> statistics.median([1,2,3,8,9])
>>> statistics.median([1,2,3,7,8,9])
5.0
>>> statistics.stdev([1,1.5,2,2.5,3,3.5,4,4.5,5])
1.3693063937629153
time module
time():
This function returns current system time in ticks. The ticks is number of seconds
>>> time.time()
1544348359.1183174
localtime():
>>> tk=time.time()
>>> time.localtime(tk)
asctime():
>>> tk=time.time()
>>> tp=time.localtime(tk)
>>> time.asctime(tp)
ctime():
>>> time.ctime()
sleep():
This function halts current program execution for a specified duration in seconds.
>>> time.ctime()
>>> time.sleep(20)
>>> time.ctime()
Few more built-in modules will be discussed in separate subsequent chapters of this
tutorial.
re module
threading module
cgi module
tkinter module
csv module
pickle module
socket module
sqlite3 module
json module