Skip to main content

ADVERTISEMENT

320x100

Python Practice

Real Python code, not toy examples. Build the muscle memory for the syntax you actually use.

Function Definition

A standard function with type hints and a default argument. Watch the colon and the return arrow.

Part 1 of 1
def fetch_user(user_id: int, include_deleted: bool = False) -> dict: return db.query(User).filter_by(id=user_id, deleted=include_deleted).first()
WPM 0
Accuracy 100%
Progress 0%
Streak 0 🔥
Speed Target: 30 WPM
⏱️ Start typing...

List Comprehension

A filtered list comprehension. Get comfortable with the nested brackets and the if clause.

Part 1 of 1
active_users = [user.email for user in users if user.is_active and user.verified_at is not None]
WPM 0
Accuracy 100%
Progress 0%
Streak 0 🔥
Speed Target: 35 WPM
⏱️ Start typing...

Class Definition

A class with __init__, self assignments, and a repr. The double underscores trip people up.

Part 1 of 1
class Product: def __init__(self, name: str, price: float, sku: str): self.name = name; self.price = price; self.sku = sku
WPM 0
Accuracy 100%
Progress 0%
Streak 0 🔥
Speed Target: 28 WPM
⏱️ Start typing...

Decorators

A route decorator from Flask. Stack two decorators and you need to stay sharp on the @ symbol.

Part 1 of 1
@app.route('/api/users/<int:user_id>', methods=['GET', 'POST']) @login_required def user_detail(user_id): return jsonify(get_user(user_id))
WPM 0
Accuracy 100%
Progress 0%
Streak 0 🔥
Speed Target: 28 WPM
⏱️ Start typing...

f-Strings

f-strings with expressions inside the braces. The colon format spec at the end is a common fumble.

Part 1 of 1
log_msg = f"[{timestamp:%Y-%m-%d %H:%M}] {level.upper()}: {message} (user={user_id}, duration={elapsed:.3f}s)"
WPM 0
Accuracy 100%
Progress 0%
Streak 0 🔥
Speed Target: 30 WPM
⏱️ Start typing...

Try / Except Block

Exception handling with multiple except clauses and a finally. Real-world error handling looks like this.

Part 1 of 1
try: response = requests.get(url, timeout=30) response.raise_for_status() except requests.Timeout: logger.error('Request timed out') except requests.HTTPError as err: logger.error(f'HTTP error: {err}') finally: session.close()
WPM 0
Accuracy 100%
Progress 0%
Streak 0 🔥
Speed Target: 25 WPM
⏱️ Start typing...

Dict Operations

Dict comprehension plus merging with the ** unpacking operator. The pipe merge syntax trips up Python 2 habits.

Part 1 of 1
counts = {word: text.split().count(word) for word in set(text.split())} result = {**defaults, **counts, 'total': len(text.split())}
WPM 0
Accuracy 100%
Progress 0%
Streak 0 🔥
Speed Target: 32 WPM
⏱️ Start typing...

Module Imports

A realistic import block. Typing from...import...as patterns precisely matters when you work with linters.

Part 1 of 1
from datetime import datetime, timedelta from typing import Optional, List, Dict import os import json from pathlib import Path
WPM 0
Accuracy 100%
Progress 0%
Streak 0 🔥
Speed Target: 35 WPM
⏱️ Start typing...

Context Manager

File I/O with a with block. Encoding parameter and exception handling included.

Part 1 of 1
with open(config_path, 'r', encoding='utf-8') as f: config = json.load(f) db_url = config.get('database', {}).get('url', os.environ['DATABASE_URL'])
WPM 0
Accuracy 100%
Progress 0%
Streak 0 🔥
Speed Target: 30 WPM
⏱️ Start typing...

Type Hints

A function with complex type annotations. Optional, List, and Union are things you type constantly in typed Python.

Part 1 of 1
def process_batch(items: List[Dict[str, any]], batch_size: int = 100) -> Optional[List[str]]: if not items: return None return [item['id'] for item in items[:batch_size]]
WPM 0
Accuracy 100%
Progress 0%
Streak 0 🔥
Speed Target: 28 WPM
⏱️ Start typing...

ADVERTISEMENT

336×280