Tags: python, backend, scripting, oop, data-science
Level: intermediate
TL;DR: Quick reference for Python core concepts, concurrency characteristics, common interview answers, and structural pitfalls.
Interpreted, high-level, dynamically typed programming language.
Focuses on readability and uses strict whitespace indentation for block structuring.
Supports multiple paradigms including object-oriented, imperative, and functional programming.
Everything is an object, including functions, modules, and classes.
Memory management relies on reference counting and a cycle-detecting garbage collector.
Global Interpreter Lock (GIL) limits multiple native threads from executing Python bytecodes simultaneously.
Extensive standard library handles async operations, network protocols, and system tasks out of the box.
Virtual environments (venv) isolate package dependencies on a per-project basis.
Q: How does memory management work in Python?R: It relies on reference counting as the primary mechanism, backed by a background garbage collector specifically designed to resolve cyclic references.
Q: What is the GIL and how does it affect concurrency?R: The Global Interpreter Lock prevents multiple native threads from executing Python bytecodes simultaneously. I use multiprocessing or async libraries to achieve true parallel execution.
Q: What are decorators and how do you use them?R: They are higher-order functions that take another function as an argument, allowing me to wrap or extend its behavior without modifying the original code.
Q: What is the difference between a list and a tuple?R: Lists are mutable and typically store homogeneous data. Tuples are immutable, hashable, and often store heterogeneous data structures.
Using mutable default arguments in functions (e.g., def foo(items=[])), which persist state across subsequent calls.
Modifying a collection while iterating over it, causing unexpected skips or runtime index errors.
Confusing shallow copies with deep copies when dealing with nested dictionaries or lists.
Catching generic exceptions with a bare except:, which intercepts system exits and keyboard interrupts.
# Virtual environment creation and activation
# python -m venv venv
# source venv/bin/activate (Linux/Mac) or venv\Scripts\activate (Windows)
# List comprehension with filtering
squares = [x**2 for x in range(10) if x % 2 == 0]
# Generator expression (memory efficient)
yield_squares = (x**2 for x in range(10))
# Decorator pattern
def timer_decorator(func):
def wrapper(*args, **kwargs):
import time
start_time = time.time()
result = func(*args, **kwargs)
print(f"Execution time: {time.time() - start_time} seconds")
return result
return wrapper
@timer_decorator
def process_data(data):
return [d * 2 for d in data]
# Context manager for safe resource handling
with open('data.txt', 'r') as file:
content = file.read()