There some standard functionalities in python that are written in particular module. For using those functionalities, it is essential to import the corresponding module first.
Modules
AU
: Dec.-19, Marks 10
•
There some standard functionalities in python that are written in particular
module. For using those functionalities, it is essential to import the
corresponding module first.
•
For example : There are various functionalities available under the module
math. For instance, if you want to find out the square root of some number then
you need to import module math first and then use the sqrt function.
•
Following screenshot illustrates this idea :
•
Basically modules in python are .py files in which set of functions are
written.
•
Modules are imported using import command.
•
There are many variables and functions present in the module. We can use them
by using the import statement.
•
When we simply use import statement, then we can use any variable or function present
within that module.
•
When we use from...import statement, then we can use only selected
variables or functions present within that module.
For
example
from
math import sqrt
print("Square
root(25) = ",sqrt(25))
Output
•
If we want to use some different name for the standard function present in the
module, then we can use as keyword. Following code illustrates this -
moduleProg1.py
from
math import sqrt as my_sq_root
print("Square
root(25)= "my_sq_root(25))
Output
Square
root(25) = 5.0
>>>
•
Every module has a name. One can use the name of the module using ___name__ attribute
of the module. For example
moduelProg3.py
print("Welcome")
print("Name
of this module is:",_name_)
Output
The
_name__ is a built-in variable that is set when the program starts. If the
program is running as a script, _name__ has the value'_main__
For
every standalone program written by the user the module is always __main__.
Hence is the output.
•
We can create our own module. Every Python program is a module. That means every
file that we save using .py extension is a module.
•
Following are the steps that illustrates how to create a module –
Step
1 :
Create a file having extension .py. Here we have created a file PrintMsg.py. In
this file, the function fun is defined.
PrintMsg.py
def
fun(usr):
print("Welcome
",usr)
Step
2 :
Now open the python shell and import the above module and then call the
functionality present in that module.
Output
Step
3:
We can also call the above created PrintMsg module in some another file. For
that purpose, open some another file and write the code in it as follows –
Test.py
import
PrintMsg #importing the user defined module
PrintMsg.fun("Rupali")
#calling the function present in that module
Step
4:
Now run the test.py program and you will get the output as follows –
Example
5.4.1 Write a module for displaying the Fibonacci
series.
Solution:
Step
1 :
FibSeries.py
def
fib(n): # write Fibonacci series up to n
a,
b = 0,1
while
b < n:
print(b,
end='')
a,
b = b, a + b
print()
Step
2:
Open the python shell and import the above module and access the functionality
within it. The output will be as follows:
Example
5.4.2 Write a Python program to define a module for
swapping the two values. Then write a driver program that will call the
function defined in your swap module.
Solution
:
Step
1:
Create a module in a file and write a function for swapping the two values. The
code is as follows -
swapProg.py
def
Swap(a,b):
tempr
a = b
b
= temp
print("After
Swapping")
print(a)
print(b)
Save
and Run the above code.
Step
2:
Now create a driver program that will invoke the swap function created in above
module.
Test.py
import
swapProg
print("Swap(10,20)")
swapProg.Swap(10,20)
print("Swap('a','b')")
swapProg.
Swap('a','b')
Step
3:
Save and run the above code. The output will be as follows –
The
dir() function is an inbuilt function. It is used display functions, variables
or classes used in the module.
Following
code shows the use of dir() function in the module.
MyModule
Prog.py
def
display(usr):
print(usr)
usr
= "Admin"
display(usr)
print(dir())
Output
•
Python module is basically a file containing some functions and statements.
•
When Python file is executed directly it
is considered as main module.
•
The main module is recognized as
__main__ and provide the basis for a complete Python program.
•
The main module can import any number of
other modules. But main module can not be imported into some other module.
•
Namespace is basically a collection of different names. Different namespaces
can time but are completely isolated.
•
If two names(variables) are same and are present in the same scope then it will
cause name clash. To avoid such situation we use the keyword namespace.
•
In Python each module has its own
namespace. This namespace includes all the names of its function and variables.
•
If we use two functions having same name belonging to two different modules at
a time in some other module then that might create name clash. For instance –
Step
1 :
Create first module for addition functionality.
FirstModule.py
def
display(a,b):
return
a + b
Step
2:
Create second module for multiplication functionality.
SecondModule.py
def
display(a,b):
return
a*b
Step
3: If
we call above modules in our driver program for performing both addition and
multiplication, then we get error because of name clash for the function.
Test.py
import
FirstModule
import
SecondModule
print(display(10,20))
print(display(5,4))
Step
4:
To resolve this ambiguity we should call these functionalities using their
module names. It is illustrated as follows -
import
FirstModule
import
SecondModule
print(FirstModule.display(10,20))
print(SecondModule.display(5,4))
Output
30
20
•
There are three commonly used categories of namespaces - Global namespace,
local namespace, and built-in namespace.
•
The global namespace contains the module which is currently executing. The
local namespace is a namespace for defining the names in a local function. The
built-in namespace is a namespace in which the built in functionality can be
invoked.
Following
Python code represents all these namespaces.
import
math
def
even_number(number): #global namespace
num
= number #local namespace
if(num%2
= = 0);
return
num
else:
return
0
print("Enter
some number")
num=int(input())
if(even_number(num)!=0):
print("The
square root of",num,"is ",math.sqrt(num)) #built-in namespace
•
All the identifiers defined in the
module are public. That means the identifier defined in one module can be
invoked by other module without any restriction. ‘
•
But there are private variables. The variables that begin with two underscore
(1) are called private variable. These are the variables which are used only
within that module. Other module cannot access these private variables.
•
Even-if we write import * from modulename then all the identifiers
except the private variables can be imported.
Review Questions
1. What is a module ? Give example. AU : Dec.-19, Marks 2
2. What are modules in Python ? How will you import them ?
Explain the concept by creating and importing a module. AU : Dec.-19, Marks 10
Problem Solving and Python Programming: UNIT V: Files, Modules, Packages : Tag: Engineering Python : Python Programming - Modules
Problem Solving and Python Programming
GE3151 1st Semester | 2021 Regulation | 1st Semester Common to all Dept 2021 Regulation