Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Class XII
Computer Science(083)
(CH-3 USING PYTHON LIBRARIES)
Topic :Import Module & Different ways to import a Module
HANDOUT
Name :___________
Import module: It means how to include a module(code written in the module) in a python program, for
sharing , using , accessing etc.
import command: it is basically used to import the entire module saved as ___.py and it is simplest ,
most common way to use modules in our code.
Its syntax is: import modulename1 [,modulename2, ---------]
Example: >>> import math
To use/ access/invoke a function, you will specify the module name and name of the function-
separated by dot (.). This format is also known as dot notation.
Example: math.sqrt(25)
>>> value= math.sqrt (25) # dot notation
Or
import calculator #as done in previous example in calculator.py
value1 = calculator.add(10,12)
value2=calculator.product(10,2)
from command: it is used to import selected object or function from a module, ie. it is used to get a
specific function in the code instead of the complete module file( If we know beforehand which
function(s), we will be needing, then we may use from). For modules having large no. of functions, it is
recommended to use from instead of import.
Its syntax is: >>> from modulename import functionname [, functionname…..]
Example : >>> from math import sqrt
value = sqrt (25)
import * : import* statement can be used to import all names from a module into the current
calling (namespace) module.
Syntax: from modulename import * ( Import everything from the file)
Example: from area import * #import * gives access to all the functions in area module
print(circle_area(12))
print(square_area(12))
print(rectangle_area(12,13))
Module Aliasing : We can create an alias when you import a module by using as keyword. Thus,
Python provides the salient feature of renaming a module at the time of import.
Member Aliasing : Like module aliasing, member aliasing is also supported and implemented in
Python modules.
Once we have defined an alias name, we should use alias name only and use of original name
should be avoided as it may create ambiguity and may result in an unexpected output or sometimes
even an error.
Example: from calculator import x as x1, add as sum
print(x1)
sum(100,201)
output : ===== RESTART: C:/Users/Desktop/programs python/memberaliasing.py =====
100
The sum is: 301
Namespace : A namespace is a system to have a unique name for each and every object in Python.(An
object might be a variable or a method).
Python itself maintains a namespace in the form of a Python dictionary.
Example: A directory-file system structure in computers. Needless to say, that one can have
multiple directories having a file with the same name inside of every directory. But one can get the
directed file, one wishes, just by specifying the absolute path to the file.
On the similar lines, Python interpreter understands what exact method or variable one is trying to
point to in the code, depending upon the namespace.
So, the division of the word itself gives little more information. Its name (which means name, an
unique identifier) + space (which talks something related to scope).
Here, a name might be of any Python method or variable and space depends upon the location from
where is trying to access a variable or a method.
Types of Namespace :
There are three types of Python namespaces:
1. Global namespace 2.Local namespace 3.Built-in namespace
It is the same with a variable scope in Python.
When Python interpreter runs solely without user-defined functions, methods, etc. Some functions
like print(), id() are always available, these are built-in namespaces
When user creates a module , a global namespace gets created.
Then creation of local functions creates the local namespace.
The built-in namespace encompasses global namespace and global namespace encompasses
local namespace.
Built-in
Global
Enclosed
Local