0% found this document useful (0 votes)
7 views8 pages

2.python Modules

Uploaded by

SARIKA NAIR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

2.python Modules

Uploaded by

SARIKA NAIR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Python Modules

Python modules serve multiple purposes, including code reuse and


facilitating the development and maintenance of large programs. They offer
a means to separate implementation details from the main program,
resulting in improved code readability and modifiability. By utilizing modules,
developers can effectively organize and manage their code, promoting
reusability and enhancing the overall efficiency of the programming process.
Python Modules can contain functions, classes, variables, and other
objects, and can be imported into other programs to be used. The definitions
and statements in a module can be accessed by other programs by using the
import statement, followed by the name of the module. Python also provides
several built-in modules that are always available for use, and users can
create their own modules by saving their functions and variables in a file
with a .py extension.
Python - Built-in Modules:
The Python interactive shell has a number of built-in functions. They
are loaded automatically as a shell starts and are always available, such as
print ( ) and input ( ) for I/O, number conversion functions int ( ), float ( ),
complex ( ), data type conversions list ( ), tuple ( ), set ( ), etc.
In addition to built-in functions, a large number of pre-defined
functions are also available as a part of libraries bundled with Python
distributions. These functions are defined in modules are called built-in
modules. Built-in modules are written in C and integrated with the Python
shell. To display list of all available built – in, use following command in
Python console: help(“modules”).
A list of a few most frequently used built-in python modules is given below:
 Random
 Math
 Time
 Os
 Shutil
 Sys
 Glob
 Re
 Statistics
 Random module: The random module is a built-in module to
generate the pseudo-random variables. It can be used perform some
action randomly such as to get a random number, selecting a random
elements from a list, shuffle elements randomly, etc.
 random( ): This method returns a random float number
between 0.0 to 1.0. The function doesn't need any arguments.
Syntax:

Output:

 randint( ): This method returns a random integer between the


specified integers.
Syntax:

Output:

 choice( ): This method returns a randomly selected element


from a non-empty sequence. An empty sequence as argument
raises an Index Error.
Syntax:
Output:

 shuffle( ): This method randomly reorders the elements in a list.


Syntax:

Output:

 Math module: The math module in Python is a standard module that


provides access to various mathematical functions and constants. We
can import the module using the ‘import math’ statement. This
module presents commonly required mathematical functions are:
trigonometric functions, representation functions, logarithmic functions
and angle conversion functions.
Ex: consider the some mathematical functions and its outputs as
shown below.
 Statistics: The statistics module provides functions to mathematical
statistics of numeric data. The following popular statistical functions
are defined in this module.
 Os: It is possible to automatically perform many operating system
tasks. The OS module in Python provides functions for creating and
removing a directory (folder), fetching its contents, changing and
identifying the current directory, etc.

 Sys Module: The sys module provides functions and variables used to
manipulate different parts of the Python runtime environment.

 Re module: A regular expression is a sequence of characters that


define a search pattern in theoretical computer science and formal
language. A regex is a sequence of characters that defines a search
pattern, used mainly for performing find and replace operations in
search engines and text processors.
 Time module: Python does not have a specific data type for dates
and times. However, it provides built-in time and date modules or the
date time module that can be imported into your Python code. This
module contains several classes, offering various functions that can be
used to manipulate dates, times, and time intervals.

2. To display Current Date:

• The time module in Python provides functions for handling time-related


tasks. The time-related tasks includes, reading the current time,
formatting time, sleeping for a specified number of seconds and so on.

1 time( ) This function returns the number of seconds passed


since epoch (the point where time begins).
2 ctime( ) This function takes seconds passed since epoch as an
argument and returns a string representing local time.
3 sleep( ) This function suspends (delays) execution of the
current thread for the given number of seconds.
4 gmtime( ) This function takes the number of seconds passed
since epoch as an argument and returns struct_time
in UTC.
5 strftime( ) This function takes gmtime (or tuple corresponding to
it) as an argument and returns a string representing it
based on the format code used.

 Shutil module: Python shutil module provides the facility to perform


the high-level file operation. It can operate with the file object and
offers us the ability of copy and remove the files. It handles the low-
level semantic such creating and closing file objects after performing
all operations.

You might also like