Python Flashcards: Fundamentals
Python — Fundamentals Flashcards
30 interactive flashcards covering core Python concepts from types and control flow to metaclasses and the GIL. Press Space to flip, rate 1-4.
Additional Flashcard Topics
List Comprehensions:
[x**2 for x in range(10) if x % 2 == 0]concise syntax for creating filtered lists. Nested comprehensions flatten to single expressions.Generators:
yieldproduces values lazily. Generators consume O(1) memory vs O(n) for lists.generator_expressionsyntax:(x**2 for x in range(10)).Decorators: functions that modify other functions.
@decoratorsyntax.functools.wrapspreserves the original function’s metadata.Context Managers:
with open('file') as f:ensures cleanup. Implement via__enter__/__exit__orcontextlib.contextmanager.Magic Methods:
__init__, __str__, __repr__, __getitem__, __len__. They define how objects behave with built-in operations.
Intuition
Python is a dynamically typed, interpreted language where everything is an object, including functions, classes, and modules. List comprehensions provide concise syntax for creating lists from iterables, and generators yield values lazily (one at a time) instead of building entire lists in memory. The GIL (Global Interpreter Lock) ensures only one thread executes Python bytecode at a time, simplifying memory management but limiting CPU-bound parallelism. Python’s “batteries included” standard library is one of its greatest strengths.
Common Pitfalls
- Mutable default arguments: Defining
def f(x=[])the default list is shared across all calls, so mutations persist. UseNoneas the default and create a new list inside the function. - Shallow vs deep copy:
list.copy()orlist()creates a shallow copy, nested objects are still shared. Usecopy.deepcopy()for independent copies of nested structures. - Late binding closures: Closures in loops capture the variable by reference, not by value, the loop variable changes before the closure executes, producing unexpected results. Use a default argument to capture the current value.
- GIL limitations: The GIL prevents true parallelism for CPU-bound threads. Use
multiprocessingfor CPU-bound work,threadingfor I/O-bound work. - Name mangling:
__namein a class becomes_ClassName__nameexternally. This is not true privacy, it’s name mangling to avoid naming conflicts.
Cross-References
- Python Practice: Auto-graded problems testing the same core Python concepts.
- Python Interactive Practice: Advanced practice with async, functional, and advanced patterns.
- Packaging and Distribution: Package management and distribution best practices.
- Ruby Basics: Dynamic typing and object-oriented patterns compared across languages.
Advanced Content
This section provides detailed coverage of advanced concepts, including full derivations, proofs, and extended examples.
Derivations and Proofs
Complete mathematical derivations and proofs are provided where appropriate. Each step is explained to ensure understanding of the underlying reasoning.
Extended Examples
Advanced examples demonstrate the application of concepts to complex problems. These examples go beyond standard exam questions to develop deeper understanding.
Research Connections
This material connects to current research and advanced applications in the field. Understanding these connections provides context for the study material.
Prerequisites
Ensure you have mastered the prerequisite material before attempting this advanced content.