Python Module
Python Module
A python module can be defined as a python program file which contains a python code
including python functions, class, or variables. In other words, we can say that our python
code file saved with the extension (.py) is treated as the module. We may have a runnable
code inside the python module.
Modules in Python provides us the flexibility to organize the code in a logical way.
To use the functionality of one module into another, we must have to import the specific
module.
Example
In this example, we will create a module named as file.py which contains a function func that
contains a code to print some message on the console.
Here, we need to include this module into our main module to call the method displayMsg()
defined in the module named file.
We can import multiple modules with a single import statement, but a module is loaded once
regardless of the number of times, it has been imported into our file.
Hence, if we need to call the function displayMsg() defined in the file file.py, we have to
import that file as a module into our module as shown in the example below.
Example:
1. import file;
2. name = input("Enter the name?")
3. file.displayMsg(name)
Output:
Consider the following module named as calculation which contains three functions as
summation, multiplication, and divide.
calculation.py:
Main.py:
Output:
The from...import statement is always better to use if we know the attributes to be imported
from the module in advance. It doesn't let our code to be heavier. We can also import all the
attributes from a module by using *.
Renaming a module
Python provides us the flexibility to import some module with a specific name so that we can
use this name to use that module in our python source file.
Example
Output:
Enter a?10
Enter b?20
Sum = 30
1. import json
2.
3. List = dir(json)
4.
5. print(List)
Output:
1. reload(<module-name>)
for example, to reload the module calculation defined in the previous example, we must use
the following line of code.
1. reload(calculation)
Scope of variables
In Python, variables are associated with two types of scopes. All the variables defined in a
module contain the global scope unless or until it is defined within a function.
All the variables defined inside a function contain a local scope that is limited to this function
itself. We can not access a local variable globally.
If two variables are defined with the same name with the two different scopes, i.e., local and
global, then the priority will always be given to the local variable.
Example
1. name = "john"
2. def print_name(name):
3. print("Hi",name) #prints the name that is local to this function only.
4. name = input("Enter the name?")
5. print_name(name)
Output:
Hi David
Python packages
The packages in python facilitate the developer with the application development
environment by providing a hierarchical directory structure where a package contains sub-
packages, modules, and sub-modules. The packages are used to categorize the application
level code efficiently.
Let's create a package named Employees in your home directory. Consider the following
steps.
2. Create a python source file with name ITEmployees.py on the path /home/Employees.
ITEmployees.py
1. def getITNames():
2. List = ["John", "David", "Nick", "Martin"]
3. return List;
3. Similarly, create one more python file with name BPOEmployees.py and create a function
getBPONames().
4. Now, the directory Employees which we have created in the first step contains two python
modules. To make this directory a package, we need to include one more file here, that is
__init__.py which contains the import statements of the modules defined in this directory.
__init__.py
5. Now, the directory Employees has become the package containing two python modules.
Here we must notice that we must have to create __init__.py inside a directory to convert this
directory to a package.
6. To use the modules defined inside the package Employees, we must have to import this in
our python source file. Let's create a simple python source file at our home directory (/home)
which uses the modules defined in this package.
Test.py
1. import Employees
2. print(Employees.getNames())
Output:
We can have sub-packages inside the packages. We can nest the packages up to any level
depending upon the application requirements.
The following image shows the directory structure of an application Library management
system which contains three sub-packages as Admin, Librarian, and Student. The sub-
packages contain the python modules.
In Python, the date is not a data type, but we can work with the date objects by importing the
module named with datetime, time, and calendar.
In this section of the tutorial, we will discuss how to work with the date and time objects in
Python.
Tick
In Python, the time instants are counted since 12 AM, 1st January 1970. The
function time() of the module time returns the total number of ticks spent since 12 AM, 1st
January 1970. A tick can be seen as the smallest unit to measure the time.
import time;
#prints the number of ticks spent since 12 AM, 1st January 1970
print(time.time())
Output:
1585928913.6519969
Example
import time;
print(time.localtime(time.time()))
Output:
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=3, tm_hour=21, tm_min=21, tm_sec=40, tm_wday=4,
tm_yday=94, tm_isdst=0)
Time tuple
The time is treated as the tuple of 9 numbers. Let's look at the members of the time tuple.
1 Month 1 to 12
2 Day 1 to 31
3 Hour 0 to 23
4 Minute 0 to 59
5 Second 0 to 60
6 Day of weak 0 to 6
Example
import time
#returns the formatted time
print(time.asctime(time.localtime(time.time())))
Output:
Example
import time
for i in range(0,5):
print(i)
#Each element will be printed after 1 second
time.sleep(1)
Output:
0
1
2
3
4
To work with dates as date objects, we have to import the datetime module into the python
source code.
Consider the following example to get the datetime object representation for the current
time.
Example
import datetime
#returns the current datetime object
print(datetime.datetime.now())
Output:
2020-04-04 13:18:35.252578
Example
1. import datetime
2. #returns the datetime object for the specified date
3. print(datetime.datetime(2020,04,04))
Output:
2020-04-04 00:00:00
We can also specify the time along with the date to create the datetime object. Consider the
following example.
Example
1. import datetime
2.
3. #returns the datetime object for the specified time
4.
5. print(datetime.datetime(2020,4,4,1,26,40))
Output:
2020-04-04 01:26:40
In the above code, we have passed in datetime() function year, month, day, hour, minute, and
millisecond attributes in a sequential manner.
Example
Output:
fun hours
Consider the following example to print the calendar for the last month of 2018.
Example
import calendar;
cal = calendar.month(2023,11)
#printing the calendar of December 2018
print(cal)
Output:
March 2020
Mo Tu We Th Fr Sa Su
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Example
import calendar
#printing the calendar of the year 2019
s = calendar.prcal(2023)
Output: