PEP 308: Conditional Expressions - The Ternary Operator
TL;DR
PEP 308 introduced conditional expressions in Python 2.5 with the syntax value_if_true if condition else value_if_false, providing a concise way to assign values based on conditions.
PEP 308 introduced conditional expressions in Python 2.5 with the syntax value_if_true if condition else value_if_false, providing a concise way to assign values based on conditions.
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.
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.
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.
Python modules are files containing Python code that can be imported and reused, organized into packages (directories with init.py), with various import styles (import, from…import, as) and special attributes like name and all.
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.
PEP 484 introduced type hints to Python, enabling static type checking with tools like mypy while maintaining runtime flexibility and improving code documentation and IDE support.
PEP 289 introduced generator expressions in Python 2.4, providing memory-efficient alternatives to list comprehensions using lazy evaluation with syntax (expression for item in iterable if condition).
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.
The functools module provides utilities for functional programming including partial(), lru_cache(), singledispatch(), and reduce() for creating reusable, optimized higher-order functions.
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!
The enum module provides Enum, IntEnum, Flag, and auto() for creating robust enumerations that replace magic numbers and strings with type-safe, self-documenting constants.
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.
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.
Python’s control flow tools include if/elif/else statements, for and while loops, break/continue statements, and the else clause for loops, providing powerful ways to control program execution.
The re module provides regular expression operations for pattern matching, searching, and text manipulation with functions like search(), match(), findall(), and sub().
PEP 20 presents the “Zen of Python” - 19 guiding principles that capture Python’s design philosophy, emphasizing readability, simplicity, and explicit over implicit approaches.
Python’s built-in functions like map(), filter(), zip(), and enumerate() provide powerful, memory-efficient ways to process data without explicit loops.
The any() and all() functions can short-circuit evaluation - any() returns True as soon as it finds one truthy value, making them incredibly efficient for large datasets.
Python classes bundle data and functionality together, supporting inheritance, method overriding, and special methods for creating powerful, reusable object-oriented code.
Python’s “everything is an object” philosophy means even classes are objects - you can inspect class attributes, pass classes as arguments, and create classes dynamically at runtime.
PEP 343 introduced the ‘with’ statement for automatic resource management, ensuring cleanup code runs even when exceptions occur, making file handling and resource management safer and cleaner.
The json module provides simple methods for converting between Python objects and JSON strings using loads() to parse JSON and dumps() to create JSON, essential for web APIs and data storage.
The itertools module provides memory-efficient iterator building blocks like chain(), combinations(), cycle(), and count() for creating powerful iteration patterns.
The itertools.product() function can generate Cartesian products infinitely - perfect for nested loops without the nesting complexity.
Python’s built-in data structures - lists, dictionaries, sets, and tuples - provide powerful tools for organizing data with different performance characteristics and use cases.