Python Module
Python Module
Python Modules
Python Module
• A file containing a set of functions you want to include in your
application.
>>> help('modules')
Built-in Modules – Frequently used
• os module
• random module
• math module
• time module
• sys module
• statistics module
OS Module
• This module has functions to perform many tasks of operating
system.
• mkdir()
• chdir()
• getcwd()
• rmdir()
• listdir()
OS Module
• mkdir()
create a new directory using mkdir() function from os module.
>>> import os
>>> os.mkdir(“Path:\\dir_name")
>>> import os
>>> os.chdir(“<path>:\\temp")
OS Module
getcwd()
• This function in returns name off current working directory.
>>> os.getcwd()
'd:\\temp’
OS Module
• 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 Module
• listdir()
The os module has listdir() function which returns list of all files
in specified directory.
Random module
• Python’s standard library contains random module which defines
various functions for handling randomization
• random.random()
Returns a random float number between 0.0 to 1.0
The function doesn’t need any arguments
>>> random.randrange(1,10)
2
>>> random.randrange(1,10,2)
3
>>> random.randrange(0,101,10)
40
Random Module
• random.choice()
• Returns a randomly selected element from a sequence object such as
string, list or tuple.
• An empty sequence as argument raises IndexError
>>> numbers=[12,23,45,67,65,43]
>>> random.shuffle(numbers)
>>> numbers
[23, 12, 43, 65, 67, 45]
>>> random.shuffle(numbers)
>>> numbers
[23, 43, 65, 45, 12, 67]
Math module
• This module presents commonly required mathematical functions.
• trigonometric functions
• representation functions
• logarithmic functions
• angle conversion functions
Math Module
• radians(<number>): converts angle in degrees to radians
• degrees(<number>): converts angle in radians to degree.
>>> math.ceil(4.5867)
5
>>> math.floor(4.5687)
4
sys module
• This module provides functions and variables used to manipulate
different parts of the Python runtime environment
sys module
sys.argv
• This function returns current system time in ticks. The ticks is number
of seconds elapsed after epoch time i.e. 12.00 am, January 1, 1970.
• >>> time.time()
1544348359.1183174
Getting time string from seconds
• time.ctime()
Delaying Execution of programs
• Execution can be delayed using time.sleep() method.
• This method is used to halt the program execution for the time
specified in the arguments
Delaying Execution of programs
Creating a Time Delay in seconds
Creating a Time Delay in minutes
Python Packages
• This file can be left empty but we generally place the initialization
code for that package in this file.
Python Packages - example
Importing module from a package
• can import modules from packages using the dot
(.) operator
Importing module from a package
• can import the module without the package prefix as follows:
Importing module from a package