Python Libraries and Modules

Libraries and Modules

Python makes it a lot easier to write code when compared to other popular programming languages as it provides extensive libraries and modules for every operation which helps to make the program shorter. For every task, we have libraries and modules provided by default in Python. For every task when we input the module then only we can use the functionality. Any python file like a group of functions, variables or classes is known as a module. 

Libraries are a collection of a group of a large number of such modules. A module is a low-level component of the library. These modules are present in the libraries and just need to be imported when we need to use them.

For instance, if we need to carry out an arithmetic operation in the program then we will need to import the math module from the library and then we can carry out any arithmetic operation required. 

Example:

>>>import math
>>>print(math.sqrt(16))
>>>print(math.pi)

If we simply use the function math.sqrt then we will not get the expected output and an error will be displayed.


Aliasing:

When instead of using the default name for the module we use any other name then this is known as aliasing.

Example:

>>>import math as m

Here in the above line of code, we tell the compiler that from now on we will refer to the math module as m and so whenever we call it the next time we will use the name m instead of math.

Note: Once we define a different name other than the default name we can only use the new name of the module. Using the earlier name in the program will cause an error. 

If the need arises to import one of the specific functions from the module of a library then we can import that particular function as well.

Example:

>>>from math import pi,sqrt

This statement will import the two functions pi, sqrt from math module.

The asterisk ‘*’ can also be used instead of a module name.

Example:

>>>from math import* #importing module
>>>print(sqrt(4))
>>>print(pi)

Output:

2.0
3.141592653589793

However, there are some cases where we cannot use the Asterix ‘*’ to import packages into the program.

For instance, if we have to use some certain methods then we can know about it from the first line itself. But while using the ‘*’ in import statement it is not possible to know about the functions used in the program. To know it we will have to analyze the entire program and find out which decreases the readability. Thus, implicit import is not recommended generally. 


Math Functions:

There are many functions already available in the math module for carrying out mathematical functions which can be readily used. Some of the functions are:-

  1. sqrt()
  2. ceil()
  3. floor()
  4. pow(x,y)
  5. factorial()
  6. gcd()
  7. sin()
  8. cos()

Along with these fixed values, certain constants are also available for use in the programs. A few such commonly used constants are:

  1. pi = 3.14
  2. e = 2.71
  3. inf = infinity
  4. nan = not a number

Example:

from math import *
rad= int(input(“Enter radius:”))
area = pi*r**2
print(“the area of circle:”, area)

In the above program, we use the math module to get the value of the constant pi to calculate the area of the circle.

Leave a Comment

Your email address will not be published. Required fields are marked *