0 day streak
Code library

Intermediate Python exercises

Read these Intermediate Python snippets line by line — each one comes with a written breakdown of what the code does and why.

Python · Intermediate

Function Decorators

Decorators allow you to wrap another function to extend its behavior without permanently modifying it. They are often used for logging, access control, or caching.

FunctionsDesign Patterns
Open snippet →
Python · Intermediate

Parallel Iteration with zip()

The zip() function takes multiple iterables and aggregates them into tuples. This is the most efficient way to iterate over multiple lists in parallel.

SyntaxPerformance
Open snippet →
Python · Intermediate

Creating Custom Exceptions

Defining custom exceptions makes your error handling more specific and readable. It allows users of your code to catch particular errors rather than generic ones.

Error HandlingBest Practices
Open snippet →
Python · Intermediatedjango

Django CBV ListView with Filtered QuerySet

ListViews are powerful Class-Based Views in Django that automatically render a list of objects. This example demonstrates how to override get_queryset() to filter articles by status and search quer…

FrameworksBest PracticesData Types
Open snippet →
Python · Intermediatedjango

Django Middleware for Request Timing

Middleware in Django sits between the web server and your views, processing every request/response. This custom middleware measures and logs the execution time of each request, adding the duration…

FrameworksBest PracticesPerformance
Open snippet →
Python · Intermediatedjango

Django Signal for Automatic Slug Generation

Django signals allow you to execute code when certain events occur in the ORM. This pre_save signal automatically generates a unique URL-friendly slug from the product name, handling duplicates by…

FrameworksDesign PatternsBest Practices
Open snippet →
Python · Intermediatedjango

Django REST Framework ViewSets and Routers

ViewSets combine list, create, retrieve, update, and destroy actions into a single class. Routers automatically generate URL patterns, reducing boilerplate and ensuring consistent URL structure acr…

FrameworksDesign PatternsBest Practices
Open snippet →
Python · Intermediatedjango

Django ModelForm Custom Validation with clean()

The clean() method validates across multiple fields. It raises ValidationError for invalid combinations. This runs after field-specific clean methods and enables complex business rule validation.

Error HandlingBest PracticesFrameworks
Open snippet →
Python · Intermediatedjango

Django Database Transactions with atomic()

atomic() ensures all operations succeed or all fail together - critical for financial data. select_for_update() locks rows to prevent race conditions during concurrent modifications.

PerformanceSecurityFrameworks
Open snippet →
Python · Intermediatedjango

Django Generic Relations with ContentType

Generic relations allow a model to relate to any other model without hardcoded foreign keys. ContentType tracks all models, and GenericForeignKey dynamically resolves the target at runtime.

FrameworksDesign PatternsOOP
Open snippet →
Python · Intermediatedjango

Django Q Objects for Complex Database Queries

Q objects enable complex database queries by combining conditions with AND, OR, and NOT operations. They are essential for building dynamic filters where conditions change based on user input. The…

FrameworksData TypesPerformance
Open snippet →
Python · Intermediatedjango

Django Session-Based Flash Messages

Django's messaging framework uses sessions to store temporary messages that display once and then disappear. The messages module provides convenience methods for different message levels (success,…

FrameworksBest PracticesError Handling
Open snippet →
Python · Intermediatedjango

Django URL Configuration with Namespace and Names

URL namespacing in Django prevents name collisions when multiple apps use similar URL names like 'home' or 'detail'. The app_name variable in app's urls.py combined with namespace in project urls.p…

FrameworksBest PracticesDesign Patterns
Open snippet →