Modules
Modules
Modules
BY
RAJESHWARI S
CONTENT
Definition
Importing Modules
Built-in Modules
DEFINITION
A module is a file contains proper python code with .py extension.
It contain Python statements and definitions.
They usually contain arbitrary objects such as classes, functions, files
and attributes.
We can use modules to break down complex programs into small
manageable and organized files.
Modules can be imported from other modules using the import
command, instead of copying their definitions and statements into
different programs.
CREATING MODULES
A module is a python file that has only definitions of variables, functions and classes.
Create a module with the suffix.py and then import it using the import command.
Examples:
#create a module calculator.py containing definitions of functions called ‘add’ and ‘multipy’.
def add(x,y):
total=x+y
return total
def multiply(x,y):
product=x*y
return product
IMPORTING MODULES
Python modules are essentially reusable libraries of code that can be imported and used in a
program.
Python interpreter is shipped with many standard library modules.
The import statement reads a module file and creates a module object, i.e., it makes a module
and its contents available for use.
There are three way to do it,
To import the entire module by including import module_name.
To import the entire module by writing import module_name as new_name.
To import specific functions from the module by writing, from module_name import name,
[,name2[,…..nameN]]
1.THROUGH A DOT(.) OPERATOR
To access the definitions in a module, we can use a dot operator between the
module name and the function we want to access.
Syntax:
import module_name
where, module_name – built-in module in python
Example:
Output:
2.IMPORT WITH RENAMING
We can import built-in or user defined modules with an alias name.
Syntax:
import module_name as new_name
where, module_name – built in module, new_name – user defined name
Example:
Output:
3.FROM……..IMPORT STATEMENT
We can import specific function names from a module without importing the module as a whole.
If we want to import only the pi function, from…..import statement can be used.
Syntax:
Python has several built-in modules that we can import for use
in programs.
Some of the most commonly used ones are
Math module
Random module
Time and Calendar module
1.MATH MODULE
Importing Python’s math module allows users to access its attributes, constants, and
mathematical function such as pi, square root, cosine and GCD.
After importing the math module, we can use the dot operation to specify a method or
attribute.
Syntax:
import math
Example: Output:
2.RANDOM MODULE
This function is used for simulation of games, testing, privacy, and security
applications.
Some of the useful functions are
Choice
Randrange
Random( )
Randint
seed( )
shuffle( )
Uniform(x,y)
CHOICE
The choice( ) function generates a random item from a sequence values.
Syntax:
random.choice(seq)
here, seq - It must be a string, list or tuple
Example:
Output:
RANDRANGE
The randrange ( ) function generates a random element from a specified range.
Syntax:
random.randrange(start,stop[,step])
Example:
Output:
RANDOM( )
It returns random float value(r), such that o is less than or equal to that value and should
be less than 1.
Syntax:
random( )
Example:
Output:
RANDINT
It is used to generate a random integer N such that x<=N<=y.
It accept two parameters (x&y), the first one (x) is the lowest number and the second
one(y) is the highest number.
Syntax:
randint(x,y)
Example:
SEED( )
The built-in method seed( ) sets the integer starting value for generating random
numbers. We must call this function before calling any random module function.
Syntax:
seed([x])
Example:
Output:
SHUFFLE( )
This function shuffles the elements on a list in random order.
Syntax:
random.shuffle(list)
Example:
Output:
Example:
Output:
UNIFORM(X,Y)
It generates the numbers from a uniform distribution. It returns a random floating point number N such that
x<=N<=y for x<=y and y<=N<=x for y<x.
The end point value y may or may not be included in the range depending on floating point rounding in the
equation x+(y-x)* random( ).
Syntax:
uniform(x,y)
Example:
Output:
3.TIME AND CALENDAR MODULE
The data time module of the python standard library provides the data time class. Most program require data
and time data to operate.
Python has time and calendar module that can be used to track times and dates in programs. To access
functions in the module, we need to import time module.
Import Time
Formatted Time
Getting : Monthly calendar
Time Module
Calendar Module
Data and Time
IMPORT TIME
Time values are represented with the time class. Times have attributes for hour, minutes,
second and microsecond.
Many time functions return a time value as a tuple of integers.
The functions strptime( ) and gmtime( ) also provide attribute names for each field.
Example: Output:
FORMATTED TIME
We can format time in many different ways with the print function. There are also
several ways to get readable time format in Python.
A simple way to get time is with the asctime( ) function.
Example:
GETTING : MONTHLY CALENDAR
The calendar module provides several methods that allow users to obtain and
manipulate monthly and yearly calendars. TO access these methods, we have to import
the calendar module.
Syntax:
import calendar
Example: Output:
TIME MODULE
Python’s time module provides functions that allows users to
work with time and convert between representations.
The list of available methods:
time.altzone
time.asctime
time.clock
time.ctime
time.gmtime
time.localtime
time.mktime
time.sleep
time.strftime(fmt[,tuppletime])
time.time
time.tzset
CALENDAR MODULE
Calendar module provides functions related to a calendar including print
functions that output a calendar for a specified year or month.
By default,
Calendar has Monday as the first day of the week. We can change this
by calling the calendar .setfirstweekday( ) functions. The calendar module
offers the following functions:
calendar.calendar (year, w=2,l=1, c=6)
calendar.firstweekday( )
calendar.isleap (year)
calendar.month (year, month, w=2,l=1)
calendar.leapdays (y1,y2)
calendar.monthcalendar(year,month)
calendar.monthrange(year,month)
calendar.prmonth(year,month,w=2, l=1)
calendar.setfirstweekday(weekday)
calendar.weekday(year,month,day)
DATE AND TIME
Python has another method, datatime ( ) that we can use to retrieve current system
data and time.
The datatime module provides us with objects which we can use to store
information about dates and times:
Datetime.date is used to create dates which are not associated with a time.
Datetime.time is used for times which are independent of a date.
Datetime.datetime is used for objects which have both a date and a time.
Datetime.timedelta: objects store differences between dates or datetimes, if we
subtract one datetime from another, the result will be a timedelta.
Datetime.timezone objects represent time zone adjustments as offsets from
UTC. This class is a subclass of datatime.tzinfo, which is not meant to be used
directly.
Example:
Example:
Example:
Output:
THANK YOU