Python Built-in Libraries

1/1/1970

Python Built-in Libraries

Python Inbuilt Libraries

1. Data Types & Structures

2. File Handling & I/O

3. Algorithms & Computations

4. Networking & Internet

5. Testing & Debugging

6. Data Serialization

7. Regular Expressions

8. Cryptography & Security

9. Concurrency & Parallelism

10. Miscellaneous


datetime Module

For working with dates and times.

Importing

from datetime import datetime, date, timedelta

Current Date & Time

now = datetime.now()               # Current date & time
today = date.today()               # Current date (without time)
print(now, today)

Formatting Dates

formatted = now.strftime("%Y-%m-%d %H:%M:%S")  # Custom format
print(formatted)                               # e.g., 2023-08-27 14:35:22

Common Format Codes:

Parsing Strings to Dates

date_obj = datetime.strptime("2023-08-27", "%Y-%m-%d")
print(date_obj)  # 2023-08-27 00:00:00

Date Arithmetic

tomorrow = today + timedelta(days=1)
print(tomorrow)  # Next day's date

time Module

For working with time and delays.

Importing

import time

Current Time

print(time.time())  # Epoch time (seconds since 1970)
print(time.ctime()) # Human-readable (e.g., "Mon Aug 28 14:00:00 2023")

Sleep (Delay Execution)

print("Start")
time.sleep(3)  # Pause for 3 seconds
print("End")

Measure Execution Time

start = time.time()
time.sleep(2)
end = time.time()
print(f"Elapsed: {end - start:.2f} seconds")

Formatting Time

now = time.localtime()
print(time.strftime("%Y-%m-%d %H:%M:%S", now))  # 2023-08-28 14:00:00

os Module

For interacting with the operating system.

Importing

import os

File and Directory Operations

print(os.getcwd())          # Current working directory
os.mkdir("new_folder")      # Create a directory
os.rmdir("new_folder")      # Remove an empty directory
os.remove("file.txt")       # Delete a file

Path Operations

print(os.path.exists("file.txt"))         # Check if a file exists
print(os.path.join("folder", "file.txt")) # Join paths safely

Accessing Environment Variable

api_key = os.getenv('API_KEY') # Get Environment Variable
db_user = os.getenv('DB_USER', 'default_user') # Default Value if Variable is Missing

Environment Variables

# Set new environment variable
os.environ['API_KEY'] = '123'  
 
# Accessing Environment Variable
print(os.environ['API_KEY'])    # Access environment variable
print(os.getenv('API_KEY')) # or Another Method to Access ⭐
print(os.getenv('API_KEY', 'default_user')) # Default Value if Variable is Missing

Or Setting Environment Variables from the OS (Outside Python)

# Setting Environment Variables - Temporarily (For Current Session)
export API_KEY="your_api_key" # Linux/macOS
set API_KEY="your_api_key" # Windows (Command Prompt)
$env:API_KEY="your_api_key" # Windows (PowerShell)

csv Module

For handling CSV (Comma-Separated Values) files.

Importing

import csv

Read CSV File

with open("data.csv", "r") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

Write CSV File

data = [["Name", "Age"], ["Alice", 30], ["Bob", 25]]
 
with open("output.csv", "w", newline='') as file:
    writer = csv.writer(file)
    writer.writerows(data)

Read CSV as Dictionary

with open("data.csv", "r") as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(row['Name'], row['Age'])

Write CSV from Dictionary

data = [{'Name': 'Alice', 'Age': 30}, {'Name': 'Bob', 'Age': 25}]
 
with open("output.csv", "w", newline='') as file:
    fieldnames = ['Name', 'Age']
    writer = csv.DictWriter(file, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerows(data)

json Module

For working with JSON (JavaScript Object Notation) data.

Importing

import json

Read JSON from File

with open("data.json", "r") as file:
    data = json.load(file)
    print(data)

Write JSON to File

data = {"name": "Alice", "age": 30}
 
with open("output.json", "w") as file:
    json.dump(data, file, indent=4)

Convert JSON String to Python

json_str = '{"name": "Bob", "age": 25}'
data = json.loads(json_str)
print(data['name'])

Convert Python to JSON String

data = {"name": "Charlie", "age": 35}
json_str = json.dumps(data, indent=4)
print(json_str)

math Module

For mathematical functions.

Importing

import math

Basic Operations

print(math.sqrt(25))      # Square root (5.0)
print(math.pow(2, 3))     # Power (2^3 = 8.0)
print(math.factorial(5))  # Factorial (120)
print(math.gcd(12, 15))   # Greatest Common Divisor (3)

Constants

print(math.pi)      # π (3.141592653589793)
print(math.e)       # Euler’s number (2.718281828459045)
print(math.inf)     # Infinity

Rounding & Trigonometry

print(math.floor(3.7))    # Round down (3)
print(math.ceil(3.2))     # Round up (4)
 
print(math.sin(math.pi/2)) # Sine (1.0)
print(math.degrees(math.pi)) # Radians → Degrees (180.0)
print(math.radians(180))     # Degrees → Radians (π)

http Module

For HTTP requests and server utilities.

Importing

from http.client import HTTPConnection

Simple GET Request

conn = HTTPConnection("example.com")
conn.request("GET", "/")
response = conn.getresponse()
print(response.status, response.reason)  # 200 OK
print(response.read().decode())          # Response body
conn.close()

urllib Module

For URL handling and web requests.

Importing

import urllib.request
import urllib.parse

Open a URL

response = urllib.request.urlopen("http://example.com")
print(response.read().decode())  # Page content

Send Data (POST Request)

data = urllib.parse.urlencode({'name': 'Alice'}).encode()
response = urllib.request.urlopen("http://example.com", data=data)
print(response.read().decode())

Download a File

urllib.request.urlretrieve("http://example.com/image.jpg", "image.jpg")

re Module

For regular expressions (pattern matching).

Importing

import re

Search for a Pattern

text = "My phone number is 123-456-7890"
match = re.search(r'\d{3}-\d{3}-\d{4}', text)
if match:
    print(match.group())  # 123-456-7890

Find All Matches

emails = re.findall(r'\w+@\w+\.\w+', "Contact us at a@test.com or b@xyz.com")
print(emails)  # ['a@test.com', 'b@xyz.com']

Replace Text

new_text = re.sub(r'\d+', 'XXX', 'Order 123 has been shipped.')
print(new_text)  # Order XXX has been shipped.

Split Text

words = re.split(r'\W+', "Python is awesome!")
print(words)  # ['Python', 'is', 'awesome', '']

asyncio Module

For asynchronous programming (I/O-bound tasks).

Importing

import asyncio

Simple Async Function

# Define an asynchronous function using 'async def'
async def greet():
    await asyncio.sleep(1)  # Non-blocking sleep for 1 second (simulates I/O operation).
    print("Hello, World!")  # Prints after the 1-second delay.
 
# Run the asynchronous function using 'asyncio.run()'
asyncio.run(greet())  # Starts the event loop and runs the 'greet()' coroutine.

Run Multiple Tasks

async def task(name, delay):
    await asyncio.sleep(delay)
    print(f"Task {name} finished!")
 
# Main coroutine to run multiple tasks concurrently
async def main():
	# Run both tasks concurrently and wait for both to complete
    await asyncio.gather(task(1, 2), task(2, 1))
    
# Start the asyncio event loop and execute 'main()'
asyncio.run(main())

Asynchronous HTTP Request

import aiohttp # For making asynchronous HTTP requests.
 
# Define an asynchronous function to fetch content from a URL
async def fetch(url):
	# Create an asynchronous HTTP session
    async with aiohttp.ClientSession() as session:
	    # Make an asynchronous GET request to the URL
        async with session.get(url) as resp:
	        # Wait for and print the response body (text content)
            print(await resp.text())
 
# Start the event loop and run the 'fetch()' coroutine
asyncio.run(fetch('http://example.com'))