Python provides a vast collection of built-in modules that help developers perform common tasks without installing external libraries. These modules cover file handling, math operations, system interactions, and more.
1. What are Built-in Modules?
Pre-installed with Python – No need for additional installation.
Optimized and Efficient – Written in C for performance.
Wide Variety – Cover OS operations, math, date/time, randomization, and more.
1.1 Importing a Built-in Module
To use a module, import it using import module_name
:
import math
print(math.sqrt(25)) # Output: 5.0
You can also import specific functions:
from math import sqrt
print(sqrt(25)) # Output: 5.0
2. Commonly Used Built-in Modules
2.1 math
– Mathematical Functions
The math
module provides advanced mathematical operations.
import math
print(math.pi) # 3.141592653589793
print(math.sqrt(64)) # 8.0
print(math.factorial(5)) # 120
print(math.sin(math.radians(30))) # 0.5
2.2 random
– Generating Random Values
The random
module is useful for random numbers, shuffling, and selections.
import random
print(random.randint(1, 10)) # Random number between 1 and 10
print(random.choice(['apple', 'banana', 'cherry'])) # Random item from a list
random.shuffle([1, 2, 3, 4, 5]) # Shuffle a list
2.3 datetime
– Handling Dates and Times
The datetime
module helps with date and time operations.
from datetime import datetime
now = datetime.now()
print(now) # Current date and time
print(now.strftime("%Y-%m-%d %H:%M:%S")) # Format date/time
2.4 os
– Interacting with the Operating System
The os
module allows interaction with the operating system (files, directories, environment variables).
import os
print(os.name) # System type (posix for Linux/macOS, nt for Windows)
print(os.getcwd()) # Current working directory
os.mkdir("new_folder") # Create a folder
os.rmdir("new_folder") # Remove a folder
2.5 sys
– System-Specific Functions
The sys
module provides access to system-related information.
import sys
print(sys.version) # Python version
print(sys.platform) # OS platform
sys.exit() # Exit program
2.6 time
– Time-related Functions
The time
module handles delays and time calculations.
import time
print(time.time()) # Current timestamp
time.sleep(2) # Pause execution for 2 seconds
2.7 json
– Working with JSON Data
The json
module helps parse and format JSON data.
import json
data = {"name": "Alice", "age": 25}
json_string = json.dumps(data) # Convert dictionary to JSON string
print(json_string) # {"name": "Alice", "age": 25}
parsed_data = json.loads(json_string) # Convert JSON back to dictionary
print(parsed_data["name"]) # Alice
2.8 collections
– Advanced Data Structures
The collections
module provides specialized data structures.
from collections import Counter
count = Counter("banana") # Count occurrences of characters
print(count) # Counter({'a': 3, 'n': 2, 'b': 1})
3. Listing Available Built-in Modules
To view all built-in modules:
help("modules")
4. Summary
Module | Purpose |
---|---|
math | Math functions (sqrt, pi, factorial) |
random | Generate random numbers, shuffle lists |
datetime | Work with dates and times |
os | Interact with the operating system |
sys | Get system-specific information |
time | Manage time and delays |
json | Parse and format JSON data |
collections | Advanced data structures (Counter, defaultdict) |