Python Code
Samples
Copy-ready Python snippets by topic and difficulty — short, focused, and runnable in the browser editor.
Cache LLM Completions by Hashing the Prompt in Python
A simple in-memory cache that stores LLM completions keyed by a SHA-256 hash of the prompt to avoid recomputing identical requests.
import hashlib
import json
class PromptCache:
def __init__(self):
self.cache = {}
def _hash_prompt(self, prompt: str) -> str:
return hashlib.sha256(prompt.encode("utf-8")).hexdigest()
def get(self, prompt: str) -> str | None:
key = self._hash_prompt(prompt)
return self.ca…
Chain of Thought Prompting in Python: Step-by-Step Reasoning Demo
This demo shows how to structure a function that explains its own reasoning step-by-step, mimicking chain-of-thought prompting for AI systems.
def solve_math_step_by_step(expression: str) -> str:
"""Solves a simple expression, showing each reasoning step."""
# Step 1: Parse the expression (assume "a + b" or "a - b")
parts = expression.split()
a = int(parts[0])
op = parts[1]
b = int(parts[2])
steps = []
steps.append(f"Step…
Circuit Breaker Pattern in Python for LLM API Calls
Implements a circuit breaker class that wraps LLM client calls to fail fast when the service is degrading, then recover automatically after a timeout.
import time
class CircuitBreaker:
def __init__(self, failure_threshold=3, recovery_timeout=5):
self.failure_threshold = failure_threshold
self.recovery_timeout = recovery_timeout
self.failure_count = 0
self.state = "closed"
self.last_failure_time = None
def call(self, …
Demonstrate Prompt Injection Bypass in Python
Simulate why naive system prompt filters fail against prompt injection with casing and spacing variations.
# Demonstrate why system prompts can be bypassed by simulated user input
# This demo shows a naive filter being ignored via prompt injection
def process_user_message(message, system_rules):
"""Simulate an AI that follows system rules but gets tricked."""
# Claim to check system rules
for rule in system_ru…
How to Accumulate Streamed Tokens into a Final String in Python
Accumulate a stream of tokens into a single final string by concatenating each token in sequence.
def accumulate_tokens(tokens):
"""Accumulate a stream of tokens into a single final string."""
result = ""
for token in tokens:
result += token
return result
if __name__ == "__main__":
token_stream = ["Hello", ", ", "world", "!", " This ", "is ", "accumulated."]
final_string = accumul…
How to Append Few-Shot Examples to a Prompt in Python
This code builds a complete LLM prompt by appending few-shot examples in alternating user/assistant format using a simple loop.
def append_few_shot_examples(prompt: str, examples: list[tuple[str, str]], separator: str = "\n\n") -> str:
"""Append few-shot examples to a prompt in alternating user/assistant format."""
full_prompt = prompt
for user_input, assistant_output in examples:
full_prompt = f"{full_prompt}{separator}Use…
How to Build a Data Helper for LLM Prompts in Python
A beginner-friendly helper class that flattens nested dictionaries, formats prompt templates, and safely parses JSON for AI/LLM pipelines.
import json
from typing import Any, Dict, List, Optional
class DataHelper:
"""Simple helper class for working with data in AI/LLM pipelines."""
def __init__(self, data: Optional[Dict[str, Any]] = None) -> None:
self.data = data or {}
def flatten(self, prefix: str = "") -> Dict[str, Any]…
How to Build a Prompt Template with Variable Slots in Python
Create a reusable LLM prompt template with named variable slots using Python's string.Template class and fill them with render() calls.
from string import Template
class PromptTemplate:
def __init__(self, template_text):
self.template = Template(template_text)
def render(self, **kwargs):
return self.template.substitute(**kwargs)
if __name__ == "__main__":
template = PromptTemplate(
"You are a helpful assistant …
How to Build a Simple Semantic Cache for Similar Prompts in Python
Mock a semantic cache that finds the closest matching prompt using word-overlap similarity and returns cached results above a threshold.
prompt_cache = [
"What is the capital of France?",
"How does recursion work?",
"Best practices for Python logging?",
"Explain binary search in one line.",
"How to reverse a string in Python?"
]
def normalize(text):
return " ".join(text.lower().split())
def similarity(a, b):
a_words = set(…
How to Build a System-User-Assistant Message List in Python
Use dataclasses to model a chat conversation and build the system/user/assistant message list expected by LLM APIs.
from dataclasses import dataclass, field
from typing import List
@dataclass
class Message:
role: str
content: str
@dataclass
class Conversation:
messages: List[Message] = field(default_factory=list)
def add_system(self, content: str) -> None:
self.messages.append(Message(role="system", con…
How to Build a Zero-Shot Classification Prompt in Python
Creates a prompt for zero-shot text classification by pairing input text with candidate labels and a hypothesis template.
from typing import Dict, List
def build_zero_shot_prompt(
text: str,
candidate_labels: List[str],
hypothesis_template: str = "This is about {}.",
) -> Dict[str, List[str]]:
"""Build a prompt ready for zero-shot classification."""
return {
"sequences": text,
"candidate_labels": can…
How to Build an Agent Loop with Plan, Act, Observe in Python
Implements a simple plan-act-observe loop that an AI agent uses to iteratively complete a task in an environment while storing observations in memory.
class Agent:
def __init__(self, name):
self.name = name
self.memory = {}
def plan(self, task):
return f"Plan for {task}: step 1, step 2, step 3"
def act(self, plan, environment):
return f"Executing {plan} in {environment}"
def observe(self, action_result):
sel…
How to Compute a Mock BLEU Score with n-gram Overlap in Python
Evaluate text similarity with a simplified BLEU score using word-level n-gram precision and a brevity penalty.
from collections import Counter
def bleu_score(reference, candidate, n=2):
"""
Compute a simplified BLEU score with n-gram precision and brevity penalty.
Mock demo using word-level n-grams.
"""
ref_tokens = reference.lower().split()
cand_tokens = candidate.lower().split()
# Compute n-…
How to Create a Mock LLM Judge Rubric Score in Python
Scores a response against a rubric by counting keyword matches, returning total, percentage, and per-criterion feedback.
def judge_score(response, rubric):
"""Mock LLM judge that scores a response against a rubric."""
total = 0
max_total = 0
feedback = []
for criterion, rubric_item in rubric.items():
max_points = rubric_item["max"]
description = rubric_item["description"]
# Simple mock scori…
How to Create a Mock Text Embedding with Hash in Python
Generate deterministic mock text embeddings using SHA-256 hashing and numpy, producing normalized vectors for similarity testing without an LLM.
import hashlib
import numpy as np
def mock_embed(text: str, dim: int = 10, seed: int = 42) -> np.ndarray:
"""Generate a deterministic mock embedding using a hash function.
Args:
text: Input text to embed
dim: Dimension of the output vector
seed: Seed for reproducibility
R…
How to Create a Simple Data Helper in Python for LLM Projects
Create a beginner-friendly Python class that stores, filters, and serializes data records for AI/LLM workflows.
import json
from typing import Any, Dict, List, Optional
class DataHelper:
"""Simple helper for beginners to manage data in AI/LLM projects."""
def __init__(self, data: Optional[List[Dict[str, Any]]] = None) -> None:
self.data: List[Dict[str, Any]] = data or []
def add_item(self, item: Dict[str…
How to Detect Prompt Injection in Python
Implements a regex-based heuristic in Python to flag common prompt injection attempts before sending input to an LLM.
import re
def contains_prompt_injection(user_input: str) -> bool:
# Directives to ignore previous instructions or act as system
ignore_patterns = [
r"\bignore\s+(all\s+)?previous\s+instructions\b",
r"\bdisregard\s+(all\s+)?previous\s+instructions\b",
r"\bdon'?t\s+follow\s+(any\s+)?inst…
How to Estimate Token Count in Python
Estimates tokens in a text string using a whitespace and punctuation heuristic without external libraries.
def estimate_tokens(text: str) -> int:
"""Estimate token count using whitespace and punctuation heuristics."""
if not text:
return 0
words = text.split()
total_punctuation = sum(1 for char in text if char in ".,!?;:")
special_tokens = sum(1 for char in text if char in "\n\t")
# Rough …
How to Filter Toxic Keywords in Python
Filter toxic keywords from text by replacing each occurrence with asterisks, useful as a basic guardrail for LLM inputs.
TOXIC_KEYWORDS = ["insult", "threat", "hate", "violence", "spam"]
def guardrails_filter(text: str, keywords: list[str] | None = None) -> str:
"""Filter out toxic keywords from the given text.
Args:
text: The input text to filter.
keywords: Optional keyword list. Defaults to TOXIC_KEYWORDS.
…
How to Keep Last K Turns in a Memory Buffer in Python
A TurnBuffer class using deque with maxlen to keep only the most recent k conversation turns in memory for LLM context.
from collections import deque
class TurnBuffer:
def __init__(self, k):
self.k = k
self.turns = deque(maxlen=k)
def add(self, turn):
self.turns.append(turn)
def last_k(self):
return list(self.turns)
if __name__ == "__main__":
buffer = TurnBuffer(3)
buffer.add("tu…
How to Log Prompts and Completions as JSONL Audit Files in Python
Read a JSONL file of LLM prompt–completion pairs, compute totals and averages, then write an audit summary with timestamps.
import json
from pathlib import Path
from datetime import datetime
def audit_jsonl(filepath):
logs = []
with open(filepath, encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
entry = json.loads(line)
logs.ap…
How to Mock an LLM Client in Python
Create a simple mock LLM client that returns a canned completion for testing or development without a real API.
from dataclasses import dataclass
@dataclass
class MockLLMClient:
canned_response: str = "This is a canned completion."
def complete(self, prompt: str) -> str:
return f"{self.canned_response} [to: {prompt[:20]}]"
if __name__ == "__main__":
client = MockLLMClient()
result = client.complete(…
How to Parse JSON from LLM Model Output Fence in Python
Extract and parse a JSON object from a language model's output that may be wrapped in triple-backtick fences with an optional language tag.
import json
import re
def parse_json_from_fence(text):
"""
Extract JSON object from a model output that may be wrapped in
triple-backtick fences with optional language tag.
"""
# Match content inside
How to Parse an LLM Response in Python
This code parses a JSON string from an LLM response, stripping code fences and handling common issues like whitespace, returning a Python dictionary.
import json
from typing import Any, Dict, List
def parse_llm_response(response: str) -> Dict[str, Any]:
"""Parse a JSON string from an LLM response, handling common edge cases."""
# Remove code fences if present
cleaned = response.strip()
if cleaned.startswith("
Browse by section
Each section groups closely related Python snippets.
Guide: free Python code samples library
Copy-ready Python snippets for learners and developers
PythonSkillset code samples are short, focused examples organised by topic and difficulty. Every snippet is server-rendered HTML — readable by search engines and easy to copy. Open any sample, read the notes, copy the code, then press Try in editor to run it in the browser with Pyodide.
How to use this library
- Pick a topic section — strings, lists, files, functions, and more
- Open a sample, read How it works, and copy the code block
- Run it in the IDE, tweak values, then take a related quiz or tutorial lesson
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.