Python modules
Modules in Python :
1.What is a Module ?
i)Modules are nothing but collections of different functions under a signal unit .
ii)Modules are pre-written pieces of code that are used to perform common tasks like
generating numbers,performing basic mathematical operations etc.
Till now we had learnt about functions in python which help us to reuse a particular
piece of code ,Modules goes a step ahead . " It allows you to reuse one or more functions
in your programs , even in that program that functions have not been defined " the
program in which user want to use functions defined in a module will simply import that
2.HOW TO IMPORT A MODULE ?
-SYNTAX = import module name OR from module-name import *
A module import in a program must be loaded into a memory before it can be used .
Python First search for that module in current working directory .If the module is not found
there ,then it looks for the modules in the directories specified in python-path environment
If module still not found there ,then python (Compiler) search that module in
PYTHON-INSTALLATION_SPECIFIC-PATH, If the module in not located even there then
compiler passes an error.
The From import statement
A module may contain definition for many variables or functions define in that module
But when a user wants to use only a particular function then this statement is used. when more then one function is required then comma is placed in between there names.
EXAMPLE :
from math import e
print("Value of e=",e)
OUTPUT :
Value of e = 2.718281828459045
3.WHAT IS USED OF " AS ” KEYWORD ?
- User can import a module with a different name using " as " keyword .this is particularly
particularly when a module name is long or confusing .
EXAMPLE :
from math import sqrt as square_root
print(" Square root of 49 =", square_root(49))
OUTPUT:
Square root of 49 = 7.0
4.How to find the name of a particular module ?
-Every module has a name .user can find name of module by using Syntax(__name__)
EXAMPLE:
from math import sqrt as square_root
print(" Square root of 49 = ",square_root(49))
print(__name__)
OUTPUT:
Square root of 49 = 7.0
__main__
STANDARD LIBRARY MODULES
Python supports three types of modules those written By the programmer ,those that are
pre-installed from external sources, and those that are pre-installed with python.Modules
that are pre-installed in python are called as STANDARD LIBRARY FUNCTION .
PYTHON MODULES LIST
1.string 11.doctest
2.datetime 12.unittest
3.re 13.pdb
4.time 14.argparse
5.math 15.sys
6.random 16.array
7.os
8.multiprocessing
9.subprocess
10.socket
User can use these modules for performing tasks like string parsing,data serialization ,testing,
dubbing and manipulating dates ,emails ,command line arguments etc.
I hope this article on modules is helpful for You ,in next one we will discuss about creating
Our own modules,and packages in python.
Thank u 😊