Reference library

Python Code Samples

Easy snippets you can copy, study, and run in the browser editor.

13 matches
A/B testing & experimentation easy

Difference in Differences Mock in Python

Generate mock panel data with a known treatment effect and compute a difference-in-differences estimate using group and period means.

did pandas simulation
Python
import numpy as np
import pandas as pd

# Generate mock panel data: 2 groups (control=0, treatment=1) × 2 periods (pre=0, post=1)
rng = np.random.default_rng(42)
n_per_cell = 50

data = []
for group in [0, 1]:
    for period in [0, 1]:
        # True effect: treatment increases outcome by 5 in the post period
        …
16 0 Open
A/B testing & experimentation easy

How to Build a Guardrail Metrics Monitor in Python

This code implements a mock monitor that records metric values, checks them against thresholds, and summarizes pass/alert statistics.

metrics monitoring ab-testing
Python
import random
import time
from collections import defaultdict


class GuardrailMetricsMonitor:
    def __init__(self):
        self.metrics = defaultdict(list)
        self.thresholds = {
            "prompt_toxicity": 0.8,
            "response_length": 500,
            "latency_ms": 1000,
        }

    def record(s…
15 0 Open
A/B testing & experimentation easy

How to Calculate Minimum Sample Size for a T-Test in Python

Compute the minimum sample size per group for a two-sample t-test using effect size, significance level, and statistical power.

sample-size statistics ab-testing
Python
import math
from scipy.stats import norm


def min_sample_size(effect_size, alpha=0.05, power=0.8):
    """
    Calculate minimum sample size for a two-sample t-test (equal groups).

    Args:
        effect_size: Cohen's d (standardized mean difference)
        alpha: significance level (Type I error)
        power: …
15 0 Open
A/B testing & experimentation easy

How to Create a Mock That Returns Inverse Counter Values in Python

Builds a Mock whose side_effect returns the inverse (1/count) of each Counter value, defaulting to 0.0 for unseen keys.

mock counter testing
Python
from collections import Counter
from unittest.mock import Mock

def inverse_mock(counter: Counter) -> Mock:
    """
    Return a Mock that mimics the inverse of a Counter:
    each key returns a value representing the inverse of its count.
    The Mock's side_effect maps keys to their inverse counts.
    """
    mock …
13 0 Open
A/B testing & experimentation easy

How to Define a Mock Primary Metric in Python

Define a mock primary metric object with a name, value, and unit, and serialize it to a dictionary for experimentation and testing.

metrics mock ab-testing
Python
class Metric:
    def __init__(self, name, value, unit=None):
        self.name = name
        self.value = value
        self.unit = unit

    def to_dict(self):
        result = {"name": self.name, "value": self.value}
        if self.unit:
            result["unit"] = self.unit
        return result

    def __repr…
15 0 Open
A/B testing & experimentation easy

How to Do Random Assignment in Python for A/B Tests

Assign each item to a binary group (0 or 1) with uniform probability using a small reusable function, optionally weighted, for A/B testing mocks.

random ab-testing assignment
Python
import random

def random_assignment_uniform_mock(items, weights=None):
    """Assign each item to a group (0 or 1) with uniform probability."""
    if weights is None:
        # Default: each item independently gets 0 or 1 with 50% probability
        return [random.randint(0, 1) for _ in items]
    # Optional weight…
13 0 Open
A/B testing & experimentation easy

How to Hash a User ID to an Experiment Bucket in Python

Deterministically map a user ID to one of N experiment buckets using MD5 hashing and modulo arithmetic.

hashing ab-testing bucketing
Python
import hashlib

def hash_to_bucket(user_id: str, num_buckets: int = 10) -> int:
    """Deterministically map a user_id to a bucket (0 to num_buckets-1)."""
    digest = hashlib.md5(user_id.encode("utf-8")).hexdigest()
    return int(digest[:8], 16) % num_buckets

if __name__ == "__main__":
    # Mock experiment: split…
14 0 Open
A/B testing & experimentation easy

How to Mock Stratified Assignment by Segment in Python

Simulate stratified assignment for A/B experiments by sampling a fixed proportion of units from each segment, with deterministic seeds for reproducibility.

ab-testing sampling random
Python
import random

def stratified_assignment(segments, seed=None):
    """
    Mock stratified assignment: given a dict of segment -> population size,
    return a dict of segment -> sampled unit ids (deterministic with seed).
    """
    if seed is not None:
        random.seed(seed)
    rng = random.Random(seed)
    res…
12 0 Open
A/B testing & experimentation easy

How to Mock a Confidence Interval for a Proportion in Python

Simulate a Bernoulli sample and compute a 95% confidence interval for a proportion using the normal approximation in Python.

confidence-interval simulation statistics
Python
import random
import math

def mock_ci(n=100, p_true=0.5, z=1.96, seed=42):
    """Simulate a sample proportion and compute its 95% confidence interval."""
    random.seed(seed)
    successes = sum(1 for _ in range(n) if random.random() < p_true)
    p_hat = successes / n
    se = math.sqrt(p_hat * (1 - p_hat) / n)
  …
15 0 Open
A/B testing & experimentation easy

How to Simulate Fixed-Horizon Testing in Python

Simulate a fixed-horizon experiment by labeling data before the horizon as warmup and after as active/inactive, then summarize via CSV.

ab-testing simulation csv
Python
import csv
import io


def fixed_horizon_mock(data: list[tuple[float, float, float]], horizon: int) -> str:
    """Simulate fixed-horizon testing, then summarize with CSV output."""
    output = io.StringIO()
    writer = csv.writer(output)
    writer.writerow(["day", "value", "signal", "status"])

    for day, value,…
13 0 Open
A/B testing & experimentation easy

How to create a global control holdout group in Python

This code implements a deterministic global control holdout group, randomly selecting a fraction of users to be excluded from feature rollouts for experiment validation.

ab-testing holdout global-control
Python
import random

class GlobalControl:
    def __init__(self, population_size, holdout_fraction=0.2, seed=42):
        random.seed(seed)
        self.population_size = population_size
        self.holdout_fraction = holdout_fraction
        self.holdout_size = int(population_size * holdout_fraction)
        self.holdout_…
11 0 Open
A/B testing & experimentation easy

How to hash user IDs to experiment buckets in Python

Deterministically map a user ID to an experiment bucket using MD5 hashing, ensuring stable and consistent assignment for A/B testing.

hashing ab-testing experiments
Python
import hashlib


def hash_user_to_bucket(user_id: str, num_buckets: int = 10) -> int:
    """Deterministically map a user ID to an experiment bucket (0..num_buckets-1)."""
    digest = hashlib.md5(user_id.encode("utf-8")).hexdigest()
    return int(digest, 16) % num_buckets


if __name__ == "__main__":
    mock_users …
12 0 Open
A/B testing & experimentation easy

Simulate a Ramp Rollout Percentage in Python

Simulates a percentage-based ramp rollout with deterministic seeding, returning success/failure/in-progress counts for a mock user population.

rollout simulation random
Python
import random
from enum import Enum

class RolloutStatus(Enum):
    SUCCESS = "success"
    FAILED = "failed"
    IN_PROGRESS = "in_progress"

def simulate_ramp_rollout(total_users: int, percentage: int, seed: int = 42) -> dict:
    """
    Simulates a mock ramp rollout for a given percentage of users.
    Returns sta…
14 0 Open

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

  1. Pick a topic section — strings, lists, files, functions, and more
  2. Open a sample, read How it works, and copy the code block
  3. 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.