Skip to main content Brad's PyNotes

Posts

2025

  1. CSV Module: Easy CSV File Reading and Writing

    TL;DR

    The csv module provides csv.reader(), csv.writer(), csv.DictReader(), and csv.DictWriter() for robust CSV file processing with automatic dialect detection and proper handling of quotes, delimiters, and line endings.

  2. PEP 526: Variable Annotations - Type Hints for Variables

    TL;DR

    PEP 526 introduced variable annotations in Python 3.6, allowing type hints for variables using the syntax variable: type = value, extending PEP 484’s function annotations to all variables for better code documentation and static analysis.

  3. OS Module: Operating System Interface for File and Process Operations

    TL;DR

    The os module provides os.listdir(), os.makedirs(), os.environ, os.path operations, and process management functions for cross-platform system interactions and file operations.

    Interesting!

    The os module automatically adapts path separators for different operating systems - os.path.join() uses backslashes on Windows and forward slashes on Unix, making your code truly cross-platform without any changes.

  4. Logging Module: Professional Application Logging

    TL;DR

    The logging module provides flexible, configurable logging with different levels (DEBUG, INFO, WARNING, ERROR, CRITICAL), handlers for various outputs, and formatters for customized log messages.

  5. Input Output Tutorial

    TL;DR

    Python provides multiple string formatting methods (f-strings, .format(), manual), file operations with context managers, and JSON serialization for data exchange.

    Interesting!

    F-strings can execute any Python expression inside the braces - you can even call functions and perform calculations right inside the string!

  6. Pathlib Module: Modern Path Handling Made Simple

    TL;DR

    The pathlib module provides object-oriented path handling with the Path class, replacing string-based os.path operations with intuitive methods for cross-platform file and directory manipulation.

  7. Functools Module: Higher-Order Functions and Functional Programming

    TL;DR

    The functools module provides utilities for functional programming including partial(), lru_cache(), singledispatch(), and reduce() for creating reusable, optimized higher-order functions.

    Interesting!

    The @lru_cache decorator can dramatically speed up recursive functions like Fibonacci calculations by memoizing results - turning an O(2^n) algorithm into O(n) with just one line!

  8. Errors and Exceptions Tutorial: Robust Error Handling in Python

    TL;DR

    Python handles errors through exceptions using try/except/finally blocks, with built-in exception types like ValueError, TypeError, and FileNotFoundError, plus the ability to create custom exceptions and proper error handling patterns.

  9. Asyncio Module: Asynchronous Programming with async/await

    TL;DR

    Asyncio enables asynchronous programming with async/await syntax, allowing single-threaded concurrent execution perfect for I/O-bound tasks like web requests and file operations.

    Interesting!

    Asyncio can handle thousands of concurrent connections with minimal memory overhead - a single asyncio application can often outperform traditional threaded servers by avoiding context switching costs.

  10. Math Module: Mathematical Functions and Constants

    TL;DR

    The math module provides mathematical functions like sin(), cos(), sqrt(), and constants like pi and e for scientific and mathematical computations.

    Interesting!

    The math.isclose() function solves floating-point comparison issues by checking if two numbers are “close enough” rather than exactly equal, essential for robust numerical code.