Back to Study Material Python

Built-in Modules

Python ships with an extensive standard library of built-in modules that cover a vast range of tasks. These modules are installed with Python itself, so yo...

Built-in Modules

Exploring Python Built-in Modules You Should Know

Python ships with an extensive standard library of built-in modules that cover a vast range of tasks. These modules are installed with Python itself, so you can import them without any additional setup. Knowing the most commonly used built-in modules saves you time and helps you avoid reinventing the wheel.

The math module provides mathematical functions beyond basic arithmetic. It includes math.sqrt(), math.pow(), math.ceil(), math.floor(), and constants like math.pi and math.e. It works with float values and is faster than implementing these operations yourself.

import math print(math.sqrt(16)) # 4.0 print(math.ceil(4.3)) # 5 print(math.pi) # 3.141592653589793

The random module provides pseudo-random number generation. random.randint(a, b) returns a random integer between a and b inclusive. random.choice(seq) picks a random element from a sequence. random.shuffle(lst) shuffles a list in place. random.random() returns a float between 0.0 and 1.0.

import random print(random.randint(1, 10)) print(random.choice(["apple", "banana", "cherry"]))

The datetime module handles dates and times. datetime.datetime.now() returns the current local date and time. You can format dates as strings with strftime() and parse strings into dates with strptime().

from datetime import datetime now = datetime.now() print(now.strftime("%Y-%m-%d %H:%M:%S"))

The json module converts between Python objects and JSON strings. json.dumps() serializes a Python dict to a JSON string, and json.loads() parses a JSON string back into a Python dict. json.dump() and json.load() do the same with file objects.

import json data = {"name": "Alice", "age": 30} json_string = json.dumps(data) restored = json.loads(json_string)

The sys module gives access to Python interpreter internals: sys.argv for command-line arguments, sys.path for the module search path, and sys.exit() to terminate the program.

Interview tip: Interviewers often ask which standard library modules you reach for most often. Be ready to mention os, sys, json, datetime, math, re (regular expressions), collections, and itertools. Knowing these modules demonstrates practical experience with Python development.