Modules
Defining a module
• Modules refer to a file containing Python statements
and definitions.
• A file containing Python code, for e.g.: example.py, is
called a module and its module name would
be example.
• We use modules to break down large programs into
small manageable and organized files. Furthermore,
modules provide reusability of code.
• We can define our most used functions in a module
and import it, instead of copying their definitions into
different programs.
Let us create a module. Type the
following and save it as calc.py.
Importing Module
• We can use any Python source file as a
module by executing an import statement in
some other Python source file.
• When interpreter encounters an import
statement, it imports the module if the
module is present in the search path.
• For example, to import the module calc.py, we
need to put the following command at the top
of the script :
Import as renaming
from...import statement
• We can import specific names from a module
without importing the module as a whole.
Here is an example.
Namespaces
• Variables are names (identifiers) that map to
objects. A namespace is a dictionary of
variable names (keys) and their corresponding
objects (values).
• A Python statement can access variables in
a local namespace and in the global
namespace.
• Therefore, in order to assign a value to a
global variable within a function, you must
first use the global statement.
• The statement global VarName tells Python
that VarName is a global variable.
• Python stops searching the local namespace
for the variable.
Built-in modules
• Use the command to get all built-in modules
in python
>>>help(‘modules’)
The dir( ) Function
• The dir() built-in function returns a sorted list
of strings containing the names defined by a
module.
• The list contains the names of all the modules,
variables and functions that are defined in a
module.
The globals() and locals() Functions
• The globals() and locals() functions can be used to
return the names in the global and local
namespaces depending on the location from
where they are called.
• If locals() is called from within a function, it will
return all the names that can be accessed locally
from that function.
• If globals() is called from within a function, it will
return all the names that can be accessed globally
from that function.
Packages in Python
• A package is basically a directory with Python files
and a file with the name __init__.py.
• This file can be empty, or it can contain valid
Python code. This code will be executed when a
package will be imported, so it can be used to
initialize a package
• This means that every directory inside of the
Python path, which contains a file named
__init__.py, will be treated as a package by
Python.
• It's possible to put several modules into a
Package.
# importing built-in module math
import math
# using square root(sqrt) function
contained
# in math module
print(math.sqrt(25))
# using pi function contained in
math module
print(math.pi)
# 2 radians = 114.59 degrees
print(math.degrees(2))
# 60 degrees = 1.04 radians
print(math.radians(60))
# Sine of 2 radians
print(math.sin(2))
# Cosine of 0.5 radians
print(math.cos(0.5))
# Tangent of 0.23 radians
print(math.tan(0.23))
# 1 * 2 * 3 * 4 = 24
print(math.factorial(4))
# importing built in module random
import random
# printing random integer between 0
and 5
print(random.randint(0, 5))
# print random floating point
number between 0 and 1
print(random.random())
# random number between 0 and 100
print(random.random() * 100)
List = [1, 4, True, 800, "python",
27, "hello"]
# using choice function in random module
for choosing
# a random element from a set such as a
list
print(random.choice(List))
# importing built in module datetime
import datetime
from datetime import date
import time
# Returns the number of seconds since the
# Unix Epoch, January 1st 1970
print(time.time())
# Converts a number of seconds to a date
object
print(date.fromtimestamp(454554))
5.0
3.14159265359
114.591559026
1.0471975512
0.909297426826
0.87758256189
0.234143362351
24
3
0.401533172951
88.4917616788
True
1461425771.87
1970-01-06