Cheatsheet de Python para Desarrolladores: Sintaxis, Colecciones y Lambdas
Guía de referencia rápida para Python: listas, diccionarios, comprensiones, lambdas, decorators, generadores y métodos clave de la biblioteca estándar.
1. List Comprehensions & Filtering
List and dictionary comprehensions offer a concise syntax when you want to create a new collection based on the values of an existing iterable.
# Square even numbers
squares = [x**2 for x in range(10) if x % 2 == 0]
# Output: [0, 4, 16, 36, 64]
# Dictionary comprehension
squares_map = {x: x**2 for x in range(5)}
# Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
2. Modern Structural Pattern Matching (Python 3.10+)
Match statements provide expressive pattern matching similar to switch/case in other languages but with destructuring capability.
def handle_command(command):
match command.split():
case ["load", filename]:
return f"Loading data from {filename}"
case ["save", filename]:
return f"Saving data to {filename}"
case ["quit" | "exit"]:
return "Exiting runtime"
case _:
return "Unknown command"
3. Context Managers & Safe Resource Cleanup
Always wrap network connections, database transactions, and file handles in with blocks to guarantee deterministic cleanup.
# Reading files automatically closing file handles
with open('dataset.csv', 'r', encoding='utf-8') as f:
for line in f:
process(line.strip())
# Custom context manager
from contextlib import contextmanager
@contextmanager
def timer():
import time
start = time.perf_counter()
yield
print(f"Elapsed: {time.perf_counter() - start:.4f}s")
🚀 Ruta de Aprendizaje y Modelos de Negocio Asociados
Tutorial de Python desde Cero para Principiantes
Domina la sintaxis completa, programación orientada a objetos y scripts de automatización.
Leer Guía Completa →Bot de Arbitraje Cripto Delta-Neutral ($3,500/mes)
Aprende a monetizar Python creando bots de arbitraje estadístico de baja latencia.
Ver Plan de Negocio →¿Necesitas otra hoja de trucos?
Añadimos nuevas guías de referencia cada semana según las peticiones de la comunidad.
Solicitar una Cheatsheet →