Python Built-in Modules Guide: Essential Standard Library

Master Python's built-in modules for file operations, datetime handling, math functions, and more. Complete guide with examples and best practices.

Using Built-in Python Modules

Table of Contents

1. [Introduction to Built-in Modules](#introduction) 2. [Module Import Mechanisms](#import-mechanisms) 3. [Essential Built-in Modules](#essential-modules) 4. [File and Directory Operations](#file-operations) 5. [Date and Time Handling](#datetime-operations) 6. [Mathematical Operations](#math-operations) 7. [System and Operating System Interface](#system-operations) 8. [Data Serialization](#data-serialization) 9. [Regular Expressions](#regex-operations) 10. [Networking and URLs](#networking) 11. [Best Practices](#best-practices)

Introduction to Built-in Modules {#introduction}

Built-in Python modules are pre-installed libraries that come with every Python installation. These modules provide essential functionality for common programming tasks without requiring external dependencies. They are part of Python's standard library and offer robust, tested solutions for various operations.

Benefits of Using Built-in Modules

| Benefit | Description | |---------|-------------| | No Installation Required | Available immediately with Python installation | | Optimized Performance | Written in C and optimized for speed | | Stability | Thoroughly tested and maintained by Python core team | | Cross-platform | Work consistently across different operating systems | | Documentation | Comprehensive official documentation available |

Module Import Mechanisms {#import-mechanisms}

Python provides several ways to import modules, each with specific use cases and implications.

Import Statement Variations

`python

Standard import

import os import sys import math

Import specific functions/classes

from datetime import datetime, timedelta from collections import defaultdict, Counter

Import with alias

import numpy as np # External module example import pandas as pd # External module example

Import all (not recommended for most cases)

from math import *

Conditional imports

try: import json except ImportError: import simplejson as json `

Import Statement Types

| Import Type | Syntax | Use Case | Example | |-------------|--------|----------|---------| | Module Import | import module | Import entire module | import os | | Selective Import | from module import item | Import specific items | from os import path | | Alias Import | import module as alias | Create shorter names | import datetime as dt | | Multiple Import | import mod1, mod2 | Import multiple modules | import os, sys, math |

Essential Built-in Modules {#essential-modules}

Core System Modules

`python import sys import os import platform import subprocess

System information

print("Python version:", sys.version) print("Platform:", platform.system()) print("Current working directory:", os.getcwd())

Command line arguments

print("Script arguments:", sys.argv)

Python path

print("Python path:", sys.path)

Environment variables

print("PATH environment:", os.environ.get('PATH')) `

Collections Module

The collections module provides specialized container datatypes.

`python from collections import ( defaultdict, Counter, OrderedDict, namedtuple, deque, ChainMap )

defaultdict - provides default values for missing keys

dd = defaultdict(list) dd['key1'].append('value1') dd['key2'].append('value2') print("DefaultDict:", dict(dd))

Counter - counts hashable objects

text = "hello world" counter = Counter(text) print("Character count:", counter) print("Most common:", counter.most_common(3))

namedtuple - creates tuple subclasses with named fields

Person = namedtuple('Person', ['name', 'age', 'city']) person1 = Person('Alice', 30, 'New York') print(f"Person: {person1.name}, Age: {person1.age}")

deque - double-ended queue

queue = deque(['a', 'b', 'c']) queue.appendleft('z') queue.append('d') print("Deque:", queue)

OrderedDict - maintains insertion order (Python 3.7+ dict does this too)

od = OrderedDict() od['first'] = 1 od['second'] = 2 od['third'] = 3 print("OrderedDict:", od) `

File and Directory Operations {#file-operations}

OS Module Operations

`python import os import shutil import glob from pathlib import Path

Directory operations

current_dir = os.getcwd() print(f"Current directory: {current_dir}")

Create directory

os.makedirs('test_directory/subdirectory', exist_ok=True)

List directory contents

contents = os.listdir('.') print("Directory contents:", contents)

File operations

file_stats = os.stat('example.txt') if os.path.exists('example.txt') else None if file_stats: print(f"File size: {file_stats.st_size} bytes") print(f"Last modified: {file_stats.st_mtime}")

Path operations

file_path = os.path.join('directory', 'subdirectory', 'file.txt') print(f"Joined path: {file_path}") print(f"Directory name: {os.path.dirname(file_path)}") print(f"Base name: {os.path.basename(file_path)}") print(f"Split extension: {os.path.splitext(file_path)}") `

Pathlib Module (Modern Approach)

`python from pathlib import Path

Create Path objects

current_path = Path.cwd() home_path = Path.home()

Path operations

file_path = Path('data') / 'files' / 'document.txt' print(f"File path: {file_path}") print(f"Parent directory: {file_path.parent}") print(f"File name: {file_path.name}") print(f"File stem: {file_path.stem}") print(f"File suffix: {file_path.suffix}")

File operations

if file_path.exists(): content = file_path.read_text() print(f"File content: {content}")

Directory operations

data_dir = Path('data') if data_dir.exists() and data_dir.is_dir(): for file in data_dir.iterdir(): if file.is_file(): print(f"File: {file.name}")

Glob patterns

python_files = list(Path('.').glob('*.py')) print(f"Python files: {python_files}") `

File Operations Comparison

| Operation | os module | pathlib | Notes | |-----------|-----------|---------|-------| | Current Directory | os.getcwd() | Path.cwd() | pathlib returns Path object | | Join Paths | os.path.join(a, b) | Path(a) / b | pathlib uses / operator | | Check Existence | os.path.exists(path) | Path(path).exists() | Both return boolean | | Read File | open(path).read() | Path(path).read_text() | pathlib handles file closing | | File Stats | os.stat(path) | Path(path).stat() | Similar functionality |

Date and Time Handling {#datetime-operations}

Datetime Module

`python from datetime import datetime, date, time, timedelta import calendar import time as time_module

Current date and time

now = datetime.now() today = date.today() current_time = datetime.now().time()

print(f"Current datetime: {now}") print(f"Today's date: {today}") print(f"Current time: {current_time}")

Creating specific dates

specific_date = datetime(2024, 12, 25, 15, 30, 0) date_only = date(2024, 12, 25) time_only = time(15, 30, 0)

Formatting dates

formatted_date = now.strftime("%Y-%m-%d %H:%M:%S") formatted_date_readable = now.strftime("%B %d, %Y at %I:%M %p")

print(f"Formatted date: {formatted_date}") print(f"Readable date: {formatted_date_readable}")

Parsing dates from strings

date_string = "2024-12-25 15:30:00" parsed_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S") print(f"Parsed date: {parsed_date}")

Date arithmetic

tomorrow = today + timedelta(days=1) last_week = today - timedelta(weeks=1) next_month = today + timedelta(days=30)

print(f"Tomorrow: {tomorrow}") print(f"Last week: {last_week}") print(f"Next month: {next_month}")

Time zones (requires pytz for full timezone support)

utc_now = datetime.utcnow() print(f"UTC time: {utc_now}") `

Time Module Operations

`python import time

Current timestamp

timestamp = time.time() print(f"Current timestamp: {timestamp}")

Sleep operation

print("Sleeping for 2 seconds...") time.sleep(2) print("Done sleeping")

Performance measurement

start_time = time.perf_counter()

Some operation here

sum(range(1000000)) end_time = time.perf_counter() execution_time = end_time - start_time print(f"Execution time: {execution_time:.4f} seconds")

Time formatting

local_time = time.localtime() formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", local_time) print(f"Formatted local time: {formatted_time}") `

Date/Time Format Codes

| Code | Meaning | Example | |------|---------|---------| | %Y | Year with century | 2024 | | %y | Year without century | 24 | | %m | Month as number | 01-12 | | %B | Full month name | January | | %b | Abbreviated month | Jan | | %d | Day of month | 01-31 | | %H | Hour (24-hour) | 00-23 | | %I | Hour (12-hour) | 01-12 | | %M | Minute | 00-59 | | %S | Second | 00-59 | | %p | AM/PM | AM, PM |

Mathematical Operations {#math-operations}

Math Module

`python import math import random import statistics from decimal import Decimal, getcontext from fractions import Fraction

Basic mathematical functions

numbers = [1, 4, 9, 16, 25]

Math module functions

print(f"Square root of 16: {math.sqrt(16)}") print(f"Ceiling of 4.2: {math.ceil(4.2)}") print(f"Floor of 4.8: {math.floor(4.8)}") print(f"Absolute value of -5: {math.fabs(-5)}")

Trigonometric functions

angle = math.pi / 4 # 45 degrees in radians print(f"Sin(45°): {math.sin(angle)}") print(f"Cos(45°): {math.cos(angle)}") print(f"Tan(45°): {math.tan(angle)}")

Logarithmic functions

print(f"Natural log of e: {math.log(math.e)}") print(f"Log base 10 of 100: {math.log10(100)}") print(f"Log base 2 of 8: {math.log2(8)}")

Power and exponential

print(f"2 to the power of 3: {math.pow(2, 3)}") print(f"e to the power of 2: {math.exp(2)}")

Constants

print(f"Pi: {math.pi}") print(f"Euler's number: {math.e}") print(f"Tau (2*pi): {math.tau}") `

Random Module

`python import random import secrets # For cryptographically secure random numbers

Basic random operations

print(f"Random float [0.0, 1.0): {random.random()}") print(f"Random integer [1, 10]: {random.randint(1, 10)}") print(f"Random choice from list: {random.choice(['apple', 'banana', 'cherry'])}")

Random sampling

numbers = list(range(1, 11)) print(f"Random sample of 3: {random.sample(numbers, 3)}")

Shuffle list

cards = ['A', 'K', 'Q', 'J', '10', '9', '8', '7', '6', '5', '4', '3', '2'] random.shuffle(cards) print(f"Shuffled cards: {cards[:5]}") # Show first 5

Random with different distributions

print(f"Gaussian distribution: {random.gauss(0, 1)}") print(f"Uniform distribution [1, 10]: {random.uniform(1, 10)}")

Seed for reproducible results

random.seed(42) print(f"Seeded random: {random.random()}") random.seed(42) print(f"Same seeded random: {random.random()}")

Secure random (for cryptographic purposes)

secure_token = secrets.token_hex(16) print(f"Secure token: {secure_token}") `

Statistics Module

`python import statistics

Sample data

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] grades = [85, 90, 78, 92, 88, 76, 95, 89, 84, 91]

Measures of central tendency

print(f"Mean: {statistics.mean(data)}") print(f"Median: {statistics.median(data)}") print(f"Mode: {statistics.mode([1, 2, 2, 3, 3, 3])}")

Measures of spread

print(f"Standard deviation: {statistics.stdev(grades)}") print(f"Variance: {statistics.variance(grades)}") print(f"Population std dev: {statistics.pstdev(grades)}")

Quantiles

print(f"Quartiles: {statistics.quantiles(grades, n=4)}") `

System and Operating System Interface {#system-operations}

System Module Operations

`python import sys import platform import subprocess import psutil # Note: This is not built-in, but commonly used

System information

print(f"Python version: {sys.version}") print(f"Python executable: {sys.executable}") print(f"Platform: {platform.platform()}") print(f"Architecture: {platform.architecture()}") print(f"Processor: {platform.processor()}") print(f"System: {platform.system()}") print(f"Release: {platform.release()}")

Memory information

print(f"Memory usage: {sys.getsizeof('Hello World')} bytes")

Command line arguments

print(f"Script name: {sys.argv[0]}") print(f"Arguments: {sys.argv[1:]}")

Python path

print("Python path directories:") for path in sys.path: print(f" {path}") `

Subprocess Module

`python import subprocess import shlex

Run simple command

try: result = subprocess.run(['ls', '-l'], capture_output=True, text=True, check=True) print("Command output:") print(result.stdout) except subprocess.CalledProcessError as e: print(f"Command failed with return code {e.returncode}") print(f"Error output: {e.stderr}")

Run command with shell

try: result = subprocess.run('echo "Hello from shell"', shell=True, capture_output=True, text=True) print(f"Shell output: {result.stdout.strip()}") except Exception as e: print(f"Shell command failed: {e}")

Pipe operations

try: # Equivalent to: ps aux | grep python ps_process = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE) grep_process = subprocess.Popen(['grep', 'python'], stdin=ps_process.stdout, stdout=subprocess.PIPE, text=True) ps_process.stdout.close() output, error = grep_process.communicate() print("Piped command output:") print(output) except Exception as e: print(f"Piped command failed: {e}") `

Data Serialization {#data-serialization}

JSON Module

`python import json from datetime import datetime

Sample data

data = { 'name': 'John Doe', 'age': 30, 'city': 'New York', 'hobbies': ['reading', 'swimming', 'coding'], 'married': True, 'children': None, 'address': { 'street': '123 Main St', 'zipcode': '10001' } }

Serialize to JSON string

json_string = json.dumps(data, indent=2) print("JSON string:") print(json_string)

Serialize to file

with open('data.json', 'w') as f: json.dump(data, f, indent=2)

Deserialize from JSON string

parsed_data = json.loads(json_string) print(f"Parsed data type: {type(parsed_data)}") print(f"Name: {parsed_data['name']}")

Deserialize from file

with open('data.json', 'r') as f: loaded_data = json.load(f) print(f"Loaded from file: {loaded_data['city']}")

Custom JSON encoder for datetime

class DateTimeEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, datetime): return obj.isoformat() return super().default(obj)

Data with datetime

datetime_data = { 'timestamp': datetime.now(), 'event': 'user_login' }

json_with_datetime = json.dumps(datetime_data, cls=DateTimeEncoder, indent=2) print("JSON with datetime:") print(json_with_datetime) `

Pickle Module

`python import pickle import base64

Sample complex data structure

class Person: def __init__(self, name, age): self.name = name self.age = age def __repr__(self): return f"Person('{self.name}', {self.age})"

Create complex data

complex_data = { 'person': Person('Alice', 25), 'numbers': [1, 2, 3, 4, 5], 'nested': {'a': {'b': {'c': 'deep_value'}}} }

Serialize with pickle

pickled_data = pickle.dumps(complex_data) print(f"Pickled data size: {len(pickled_data)} bytes")

Deserialize with pickle

unpickled_data = pickle.loads(pickled_data) print(f"Unpickled person: {unpickled_data['person']}") print(f"Unpickled nested value: {unpickled_data['nested']['a']['b']['c']}")

Pickle to/from file

with open('data.pickle', 'wb') as f: pickle.dump(complex_data, f)

with open('data.pickle', 'rb') as f: loaded_pickle_data = pickle.load(f) print(f"Loaded from pickle file: {loaded_pickle_data['person']}")

Base64 encoding for text representation

encoded_pickle = base64.b64encode(pickled_data).decode('ascii') print(f"Base64 encoded pickle: {encoded_pickle[:50]}...") `

Serialization Comparison

| Format | Module | Pros | Cons | Use Case | |--------|--------|------|------|----------| | JSON | json | Human readable, cross-language | Limited data types | APIs, config files | | Pickle | pickle | All Python objects | Python-only, security risks | Python-to-Python data | | CSV | csv | Simple, spreadsheet compatible | Flat data only | Tabular data | | XML | xml | Structured, self-documenting | Verbose | Document markup |

Regular Expressions {#regex-operations}

RE Module

`python import re

Sample text for pattern matching

text = """ Contact Information: Email: john.doe@example.com Phone: (555) 123-4567 Website: https://www.example.com Date: 2024-01-15 """

Basic pattern matching

email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' phone_pattern = r'\(\d{3}\)\s\d{3}-\d{4}' url_pattern = r'https?://[^\s]+' date_pattern = r'\d{4}-\d{2}-\d{2}'

Find methods

email_match = re.search(email_pattern, text) if email_match: print(f"Found email: {email_match.group()}")

Find all occurrences

all_emails = re.findall(email_pattern, text) print(f"All emails: {all_emails}")

Match with groups

phone_match = re.search(r'\((\d{3})\)\s(\d{3})-(\d{4})', text) if phone_match: area_code, exchange, number = phone_match.groups() print(f"Phone parts: Area={area_code}, Exchange={exchange}, Number={number}")

Substitution

masked_text = re.sub(email_pattern, '[EMAIL HIDDEN]', text) print("Text with masked emails:") print(masked_text)

Compiled patterns for better performance

email_regex = re.compile(email_pattern, re.IGNORECASE) matches = email_regex.finditer(text) for match in matches: print(f"Email found at position {match.start()}-{match.end()}: {match.group()}")

Split using regex

data = "apple,banana;cherry:orange" fruits = re.split(r'[,;:]', data) print(f"Split fruits: {fruits}")

Validation examples

def validate_email(email): pattern = r'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}

Python Built-in Modules Guide: Essential Standard Library

return bool(re.match(pattern, email))

def validate_phone(phone): pattern = r'^\(\d{3}\)\s\d{3}-\d{4}

Python Built-in Modules Guide: Essential Standard Library

return bool(re.match(pattern, phone))

Test validations

test_emails = ['valid@example.com', 'invalid.email', 'test@test.co.uk'] test_phones = ['(555) 123-4567', '555-123-4567', '(555)123-4567']

print("Email validation results:") for email in test_emails: print(f" {email}: {validate_email(email)}")

print("Phone validation results:") for phone in test_phones: print(f" {phone}: {validate_phone(phone)}") `

Regular Expression Patterns

| Pattern | Description | Example | Matches | |---------|-------------|---------|---------| | \d | Any digit | \d{3} | 123, 456 | | \w | Word character | \w+ | hello, world123 | | \s | Whitespace | \s+ | space, tab, newline | | ^ | Start of string | ^Hello | Hello world | | $ | End of string | world$ | Hello world | | | Zero or more | ab | a, ab, abbb | | + | One or more | ab+ | ab, abbb | | ? | Zero or one | ab? | a, ab | | [] | Character class | [aeiou] | a, e, i, o, u | | () | Group | (ab)+ | ab, abab |

Networking and URLs {#networking}

urllib Module

`python import urllib.request import urllib.parse import urllib.error from urllib.parse import urlparse, urljoin, quote, unquote

URL parsing

url = "https://www.example.com:8080/path/to/resource?param1=value1¶m2=value2#section" parsed_url = urlparse(url)

print("URL components:") print(f" Scheme: {parsed_url.scheme}") print(f" Netloc: {parsed_url.netloc}") print(f" Path: {parsed_url.path}") print(f" Query: {parsed_url.query}") print(f" Fragment: {parsed_url.fragment}")

URL manipulation

base_url = "https://api.example.com/" endpoint = "users/123" full_url = urljoin(base_url, endpoint) print(f"Joined URL: {full_url}")

URL encoding/decoding

text_with_spaces = "hello world & special chars" encoded_text = quote(text_with_spaces) decoded_text = unquote(encoded_text) print(f"Original: {text_with_spaces}") print(f"Encoded: {encoded_text}") print(f"Decoded: {decoded_text}")

Query string handling

params = { 'search': 'python programming', 'category': 'tutorials', 'limit': 10 } query_string = urllib.parse.urlencode(params) print(f"Query string: {query_string}")

Parse query string

parsed_params = urllib.parse.parse_qs(query_string) print(f"Parsed params: {parsed_params}")

HTTP requests (basic)

try: # Simple GET request response = urllib.request.urlopen('https://httpbin.org/json') data = response.read().decode('utf-8') print(f"Response status: {response.getcode()}") print(f"Response headers: {dict(response.headers)}") print(f"Response data: {data[:100]}...") # First 100 chars except urllib.error.URLError as e: print(f"URL Error: {e}") except urllib.error.HTTPError as e: print(f"HTTP Error: {e.code} - {e.reason}") `

Socket Module (Basic Networking)

`python import socket import threading import time

Get host information

hostname = socket.gethostname() local_ip = socket.gethostbyname(hostname) print(f"Hostname: {hostname}") print(f"Local IP: {local_ip}")

DNS lookup

try: google_ip = socket.gethostbyname('google.com') print(f"Google IP: {google_ip}") except socket.gaierror as e: print(f"DNS lookup failed: {e}")

Simple TCP client

def tcp_client_example(): try: # Create socket client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect to server (example: connecting to a web server) server_address = ('httpbin.org', 80) client_socket.connect(server_address) # Send HTTP request request = "GET /ip HTTP/1.1\r\nHost: httpbin.org\r\n\r\n" client_socket.send(request.encode()) # Receive response response = client_socket.recv(1024).decode() print("HTTP Response:") print(response[:200]) # First 200 characters client_socket.close() except Exception as e: print(f"TCP client error: {e}")

Simple TCP server

def tcp_server_example(): try: server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_address = ('localhost', 12345) server_socket.bind(server_address) server_socket.listen(1) print(f"Server listening on {server_address}") # Accept one connection client_socket, client_address = server_socket.accept() print(f"Connection from {client_address}") # Receive data data = client_socket.recv(1024).decode() print(f"Received: {data}") # Send response response = "Hello from server!" client_socket.send(response.encode()) client_socket.close() server_socket.close() except Exception as e: print(f"TCP server error: {e}")

Port scanning function

def scan_port(host, port): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(1) # 1 second timeout result = sock.connect_ex((host, port)) sock.close() return result == 0 # True if connection successful except: return False

Scan common ports

def scan_common_ports(host): common_ports = [21, 22, 23, 25, 53, 80, 110, 143, 443, 993, 995] open_ports = [] print(f"Scanning {host} for open ports...") for port in common_ports: if scan_port(host, port): open_ports.append(port) print(f" Port {port}: Open") return open_ports

Example usage

print("\n--- TCP Client Example ---") tcp_client_example()

print("\n--- Port Scanning Example ---") open_ports = scan_common_ports('google.com') print(f"Open ports on google.com: {open_ports}") `

Best Practices {#best-practices}

Import Best Practices

`python

Good practices for imports

1. Standard library imports first

import os import sys from datetime import datetime from collections import defaultdict

2. Related third-party imports

import requests

import numpy as np

3. Local application imports

from myapp import settings

from myapp.utils import helper_function

4. Avoid wildcard imports

Bad: from math import *

Good: from math import sqrt, pi

5. Use absolute imports when possible

Good: from mypackage.submodule import function

Avoid: from .submodule import function (except in packages)

6. Group imports logically

import json # Data serialization import pickle # Data serialization import re # Text processing import urllib.parse # URL handling `

Error Handling with Modules

`python import logging import traceback from contextlib import contextmanager

Configure logging

logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s' )

Error handling example

def safe_json_load(filename): """Safely load JSON file with proper error handling""" try: with open(filename, 'r') as f: return json.load(f) except FileNotFoundError: logging.error(f"File not found: {filename}") return None except json.JSONDecodeError as e: logging.error(f"Invalid JSON in {filename}: {e}") return None except Exception as e: logging.error(f"Unexpected error loading {filename}: {e}") logging.debug(traceback.format_exc()) return None

Context manager for resource handling

@contextmanager def file_manager(filename, mode='r'): """Context manager for file operations""" try: f = open(filename, mode) yield f except Exception as e: logging.error(f"Error with file {filename}: {e}") raise finally: if 'f' in locals(): f.close() logging.info(f"File {filename} closed")

Usage example

with file_manager('example.txt', 'w') as f: f.write("Hello, World!") `

Performance Considerations

`python import time import functools from collections import deque

Timing decorator

def timing_decorator(func): """Decorator to measure function execution time""" @functools.wraps(func) def wrapper(args, *kwargs): start_time = time.perf_counter() result = func(args, *kwargs) end_time = time.perf_counter() print(f"{func.__name__} executed in {end_time - start_time:.4f} seconds") return result return wrapper

Caching decorator

@functools.lru_cache(maxsize=128) def expensive_computation(n): """Example of cached function""" time.sleep(0.1) # Simulate expensive operation return n * n

Performance comparison

@timing_decorator def list_operations(): """Operations with list""" data = [] for i in range(10000): data.append(i) return data

@timing_decorator def deque_operations(): """Operations with deque""" data = deque() for i in range(10000): data.append(i) return data

Memory usage monitoring

import sys

def get_size(obj): """Get size of object in bytes""" return sys.getsizeof(obj)

Example usage

print("Performance comparison:") list_result = list_operations() deque_result = deque_operations()

print(f"List size: {get_size(list_result)} bytes") print(f"Deque size: {get_size(deque_result)} bytes")

Cached function example

print("\nCached function example:") print(f"First call: {expensive_computation(5)}") # Will take 0.1 seconds print(f"Second call: {expensive_computation(5)}") # Will be instant (cached) `

Module Documentation and Testing

`python """ Example module demonstrating documentation and testing practices.

This module provides utility functions for common operations. """

import doctest import unittest

def fibonacci(n): """ Generate fibonacci sequence up to n terms. Args: n (int): Number of terms to generate Returns: list: List of fibonacci numbers Examples: >>> fibonacci(5) [0, 1, 1, 2, 3] >>> fibonacci(0) [] >>> fibonacci(1) [0] """ if n <= 0: return [] elif n == 1: return [0] elif n == 2: return [0, 1] sequence = [0, 1] for i in range(2, n): sequence.append(sequence[i-1] + sequence[i-2]) return sequence

class TestFibonacci(unittest.TestCase): """Unit tests for fibonacci function""" def test_fibonacci_positive(self): """Test fibonacci with positive numbers""" self.assertEqual(fibonacci(5), [0, 1, 1, 2, 3]) self.assertEqual(fibonacci(1), [0]) self.assertEqual(fibonacci(2), [0, 1]) def test_fibonacci_zero_negative(self): """Test fibonacci with zero and negative numbers""" self.assertEqual(fibonacci(0), []) self.assertEqual(fibonacci(-1), [])

if __name__ == "__main__": # Run doctests print("Running doctests...") doctest.testmod(verbose=True) # Run unit tests print("\nRunning unit tests...") unittest.main(verbosity=2) `

Module Organization Summary

| Aspect | Best Practice | Example | |--------|---------------|---------| | Import Order | Standard, third-party, local | import os; import requests; from myapp import utils | | Error Handling | Specific exceptions | except FileNotFoundError: instead of except: | | Documentation | Docstrings and comments | Use """docstring""" format | | Testing | Unit tests and doctests | Use unittest module and doctest | | Performance | Profile and optimize | Use timeit, cProfile modules | | Logging | Use logging module | logging.info() instead of print() |

Built-in Python modules provide a comprehensive foundation for most programming tasks. Understanding these modules and following best practices ensures efficient, maintainable, and robust code development. The standard library's extensive functionality reduces the need for external dependencies while providing battle-tested solutions for common programming challenges.

Tags

  • datetime-handling
  • file-operations
  • python-imports
  • python-modules
  • standard-library

Related Articles

Related Books - Expand Your Knowledge

Explore these Python books to deepen your understanding:

Browse all IT books

Popular Technical Articles & Tutorials

Explore our comprehensive collection of technical articles, programming tutorials, and IT guides written by industry experts:

Browse all 8+ technical articles | Read our IT blog

Python Built-in Modules Guide: Essential Standard Library