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.
def equal(a, b):
if(a == b):
Also, you have written a main() function in a separate file named ‘main.py’ with the below-given code.
from equal import *
a, b = 10, 10
When you execute the main function in the command prompt, you will get the output shown below.
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.
from operator import *
a, b = 10, 10
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.
This module provides a set of pre-defined functions corresponding to operators in Python.
This module is used to print the complete decimal value when one number is divided by another number.
This module is used to generate random numbers. Some of the pre-defined functions of this module are randint(), choice(), uniform, etc.
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 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'
#prints True if the value of 'a' is greater than 'b'. Else, False
#prints the remainder value, when the value of 'a' is divided by 'b'
#concatenates and prints the given two strings
b) Example Using Decimal Module
from decimal import *
a, b = 10, 3
c = a / b
print(Decimal(c)) #prints the complete decimal value of c
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
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
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
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.