Python Built-In Modules | FACE Prep

In the article on Modules in Python, we came across Python built-in modules that are pre-defined in Python library. In this article, let us discuss these built-in modules in detail.

What are Python Built-In Modules?

Python has a large number of built-in modules. These modules can be used in Python programs by simply importing them using their name followed by the keyword ‘import’. In Python, built-in modules are written in C and integrated with the Python interpreter. 

Before understanding them in detail, let us see how they make coding much simpler. 

Why Do We Need Python Built-In Modules?

Consider you need to find out whether the given two numbers are equal or not. To do this, assume you have written a module named ‘equal.py’ with the below-given function definition.

#function definition
def equal(a, b):
    if(a == b):
        return True

Also, you have written a main() function in a separate file named ‘main.py’ with the below-given code.

#main function
from equal import *
a, b = 10, 10
print(equal(a, b))

When you execute the main function in the command prompt, you will get the output shown below.

Output:
True

In Python, each and every built-in module has a large number of predefined functions to perform specific tasks. For instance, Python has a built-in module named ‘operator’, inside which the function called ‘eq()’ is defined. This function returns the boolean value of ‘True’ if the given two input values are equal. Else, returns False.

So now, we can make use of this operator module in our main() program to check if two numbers are equal. This means we would no longer need that ‘equal.py’ module.

#main function
from operator import *
a, b = 10, 10
print(eq(a, b))
Output:
True

Next, let us discuss some of the Python built-in modules in detail.

Python Modules List

Using Python built-in function help(), we can obtain the list of built-in modules available in Python. On executing the line help (‘modules’) in Python IDE, you can see all the Python built-in modules. Some of the frequently used ones are discussed below.

Module

Description

Operator

This module provides a set of pre-defined functions corresponding to operators in Python.

decimal

This module is used to print the complete decimal value when one number is divided by another number.

random

This module is used to generate random numbers. Some of the pre-defined functions of this module are randint(), choice(), uniform, etc.

string

string module provides a set of functions that are used to perform certain operations on characters. This module has pre-defined functions like capwords, ascii_letters, etc.

math

Math module is used to perform mathematical operations. This module provides some pre-defined mathematical functions like sqrt, factorial, etc.

Next, let us see how to implement these built-in modules in the Python programs.

Python Built-In Modules Examples

a) Example Using Operator Module

from operator import *
a, b = 10, 20
#prints the product of the values 'a' and 'b'
print(mul(a, b))
#prints True if the value of 'a' is greater than 'b'. Else, False
print(gt(a, b))
#prints the remainder value, when the value of 'a' is divided by 'b'
print(mod(a, b))
#concatenates and prints the given two strings
print(concat("FACE", "Prep"))
Output:
200
False
10
FACEPrep

b) Example Using Decimal Module

from decimal import *
a, b = 10, 3
c = a / b
print(c)
print(Decimal(c)) #prints the complete decimal value of c
Output:
3.3333333333333335
3.333333333333333481363069950020872056484222412109375

c) Example Using Random Module

from random import *
print(randint(10, 20)) #prints a random number between the given range
list1 = [30, 23, 45, 16, 89, 56]
print(choice(list1)) #prints a random element from the given iterator
print(uniform(10, 20)) #prints a random float number between two given values
Output:
18
16
12.908184799437432

d) Example Using String Module

from string import *
print(capwords("fACE prep")) #capitalizes the first letter of each words
print(ascii_letters) #prints all lowercase and uppercase letters
Output:
FACE Prep
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

e) Example Using Math Module

from math import *
print(sqrt(16)) #prints the square root of the value 16 in the form of a floating-point value
print(factorial(5)) #prints the factorial of the value 5
Output:
4.0
120

Wrap up

Since Python provides a lot of built-in modules, it is advisable to use built-in modules rather than user-created modules to perform basic operations.