Unit 5 Modules
Unit 5 Modules
Prepared By:
Tanvi Patel
Asst. Prof. (CSE)
Contents
◉ Introduction to Modules
◉ Types of Modules
◉ Math Module
◉ Random Module
◉ Datetime module
◉ Sys module
◉ OS module
◉ NOTE: For math,random and datetime module refer colab shared file.
What are modules?
◉ The math module is a standard module in Python and is always available. To use
mathematical functions under this module, you have to import the module
using import math.
◉ It provides us access to some common math functions and constants in Python,
which we can use throughout our code for more complex mathematical
computations.
◉ The library is a built-in Python module, therefore you don't have to do any
installation to use it.
◉ For example,
# Square root calculation
import math
math.sqrt(4)
Math Module (Cont.)
Special Constants
◉ The Python Math Library contains two important constants.
◉ Pie
◉ The first one is Pie (π), a very popular math constant. It denotes the ratio of circumference to
diameter of a circle and it has a value of 3.141592653589793. To access it, we first import the
Math Library as follows:
◉ import math
◉ We can then access this constant using pi:
◉ math.pi
◉ Output
◉ 3.141592653589793
◉ You can use this constant to calculate the area or circumference of a circle.
Math Module (Cont.)
◉ Euler's Number
◉ The Euler's number (e), which is the base of natural logarithm is also defined in the Math library.
We can access it as follows:
◉ math.e
◉ Output
◉ 2.718281828459045
◉ The following example demonstrates how to use the above constant:
◉ import math
◉ print((math.e + 6 / 2) * 4.32)
◉ Output
◉ 24.702977498943074
Math Module (Cont.)
Arithmetic Functions
◉ Arithmetic functions are used to represent numbers in various forms and perform mathematical
operations on them. Some of the most common arithmetic functions are discussed below:
◉ ceil(): returns the ceiling value of the specified number.
◉ fabs(): returns the absolute value of the specified number.
◉ floor(): returs the floor value of the specified number.
◉ gcd(a, b): returns the greatest common divisor of a and b.
◉ fsum(iterable): returns the sum of all elements in an iterable object.
◉ expm1(): returns (e^x)-1.
◉ exp(x)-1: when the value of x is small, calculating exp(x)-1 may lead to a significant loss in
precision. The expm1(x) can return the output in with full precision.
Math Module (Cont.)
Math Module (Cont.)
◉ Trigonometric Functions
◉ The Python Math module supports all the trigonometric functions. Some of them have been
enlisted below:
◉ sin(a): Returns the sine of "a" in radians
◉ cos(a): Returns the cosine of "a" in radians
◉ tan(a): Returns the tangent of "a" in radians
◉ asin(a): Returns the inverse of sine. Also, there are "atan" and "acos".
◉ degrees(a): Converts an angle "a" from radian to degrees.
◉ radians(a): Converts angle "a" from degrees to radian.
Math Module (Cont.)
◉ Note that we first converted the value of the angle from degrees to radians before performing the
other operations.
Math Module (Cont.)
◉ Conclusion
◉ The Python Math Library provides us with functions and constants that we can use
to perform arithmetic and trigonometric operations in Python. The library comes
installed in Python, hence you are not required to perform any additional installation
in order to be able to use it. For more info you can find the official documentation
here.
Random Module
◉ random.random()
◉ This function generates a random float number between 0.0 and 1.0.
◉ random.randint()
◉ This function returns a random integer between the specified integers.
◉ random.choice()
◉ This function returns a randomly selected element from a non-empty sequence.
Random Module (Cont.)
Random Module (Cont.)
◉ random.shuffle()
This function randomly reorders the elements in the list.
◉ random.randrange(beg,end,step)
This function is used to generate a number within the range specified in its
argument. It accepts three arguments, beginning number, last number, and step,
which is used to skip a number in the range.
◉ random.seed()
This function is used to apply on the particular random number with the seed
argument.
The generator creates a random number based on the seed value, so if the seed val
ue is 10, you will always get 0.5714025946899135 as the first random number.
Random Module (Cont.)
Random Module (Cont.)
Function Description
Function Description
Function Description
◉ The datetime module is used to modify date and time objects in various ways. It contains five
classes to manipulate date and time.
◉ They are as follows:
◉ Date: deals with date objects.
◉ Time: deals with time objects.
◉ datetime: deals with date and time object combinations.
◉ timedelta: deals with intervals of time. Used in calculations of past and future date and time
objects.
◉ Info: deals with time zone info of local time.
Python datetime (Cont.)
◉ To create and modify new date and time objects, we need to import the module. We load them
by using the following statement.
◉ import datetime
◉ Or if you want only one of the modules-
◉ from datetime import date
◉ from datetime import time
◉ from datetime import datetime
◉ We could create new objects that have different date and time stored for manipulation using the
datetime module.
◉ The following syntax would do the needful.
◉ import datetime
◉ datetime.datetime(year_number, month_number, date_number, hours_number,
minutes_number, seconds_number)
Python datetime
◉ In this program, we have used today() method defined in the date class to get a date object
containing the current local date.
Python datetime (Cont.)
◉ import datetime
◉ print (datetime.date.today())
◉ Or
◉ from datetime import date
◉ print (date.today())
◉ To access individuals:
◉ now = date.today()
◉ print (now.day)
◉ print (now.month)
◉ print (now.year)
Python datetime (Cont.)
◉ You could also use the date class to find out the weekday of the current date
◉ Here is the Weekday Table which start with Monday as 0 and Sunday as 6.
◉ from datetime import date
Day WeekDay Number
◉ now = date.today()
Monday 0
◉ print (now.weekday())
Tuesday 1
Wednesday 2
Thursday 3
Friday 4
Saturday 5
Sunday 6
Python datetime (Cont.)
◉ The python sys module provides functions and variables which are used to
manipulate different parts of the Python Runtime Environment. It lets us access
system-specific parameters and functions.
◉ import sys
◉ First, we have to import the sys module in our program before running any
functions.
Sys Module (Cont.)
◉ sys.modules
◉ This function provides the name of the existing python modules which have been imported.
◉ sys.argv
◉ This function returns a list of command line arguments passed to a Python script. The name of
the script is always the item at index 0, and the rest of the arguments are stored at subsequent
indices.
◉ sys.exit
◉ This causes the script to exit back to either the Python console or the command prompt. This is
generally used to safely exit from the program in case of generation of an exception.
◉ sys.platform
◉ This value of this function is used to identify the platform on which we are working.
Sys Module (Cont.)
◉ sys.maxsize
◉ Returns the largest integer a variable can take.
◉ sys.version
◉ This attribute displays a string containing the version number of the current Python
interpreter.
◉ sys.copyright
◉ This String just displays the copyright information on currently installed Python
version.
◉ sys.path
◉ This is an environment variable that is a search path for all Python modules.
◉ sys.getrecursionlimit() #Get maximal recursion depth
OS Module
◉ Python OS module provides the facility to establish the interaction between the user
and the operating system. It offers many useful OS functions that are used to
perform OS-based tasks and get related information about operating system.
◉ The OS comes under Python's standard utility modules. This module offers a
portable way of using operating system dependent functionality.
◉ The Python OS module lets us work with the files and directories.
OS Module (Cont.)
◉ os.chdir(): The os module provides the chdir() function to change the current
working directory.
OS Module (Cont.)
◉ os.listdir(): The listdir() function lists all the folders and files in the directory.
◉ os.rmdir(): The rmdir() function removes the specified directory with an absolute or
related path. First, we have to change the current working directory and remove the
folder.
OS Module (Cont.)