Reference library

Python Code Samples

Copy-ready Python snippets by topic and difficulty — short, focused, and runnable in the browser editor.

32 matches
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 medium

How to Simulate Geo Experiments in Python

Build a mock geo experiment simulator with ramp-up/down periods, measuring weekly lift between treatment and control markets.

geo-experiment ab-testing simulation
Python
import random
import math
from dataclasses import dataclass

@dataclass
class GeoMarket:
    name: str
    base_demand: float
    geo_coefficient: float

def simulate_geo_experiment(markets, weeks=12, control_weeks=6):
    """
    Simulates a geo experiment with ramp-up and ramp-down periods.
    Returns weekly lift p…
18 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 medium

How to join assignment logs with outcomes in Python

Merge submission log entries with grading outcomes using left join and full outer join patterns in pure Python.

join data-merge ab-testing
Python
from datetime import datetime, timedelta

class AssignmentLog:
    def __init__(self):
        self.logs = [
            {"assignment_id": 101, "student_id": "S001", "submitted_at": "2024-03-01 10:30:00"},
            {"assignment_id": 101, "student_id": "S002", "submitted_at": "2024-03-02 14:15:00"},
            {"as…
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
A/B testing & experimentation medium

Thompson Sampling Mock Bandit in Python

Implement a Thompson sampling multi-armed bandit to explore and exploit reward probabilities across multiple options, updating Beta distributions over time.

thompson-sampling bandit-algorithms exploration-exploitation
Python
import random

class ThompsonSamplingBandit:
    def __init__(self, num_arms, alpha=1.0, beta=1.0):
        self.num_arms = num_arms
        self.alpha = [alpha] * num_arms
        self.beta = [beta] * num_arms

    def select_arm(self):
        samples = [random.betavariate(a, b) for a, b in zip(self.alpha, self.beta…
12 0 Open
A/B testing & experimentation medium

UCB1 Bandit Algorithm in Python

This code implements the UCB1 multi-armed bandit algorithm, balancing exploration and exploitation to identify the best arm while maximizing cumulative reward.

ucb1 bandit ab-testing
Python
import math
import random


def ucb1(means, n_iterations=1000, exploration_weight=2.0):
    """Run UCB1 bandit algorithm on arms with given true means."""
    n_arms = len(means)
    counts = [0] * n_arms
    rewards = [0.0] * n_arms
    
    for t in range(1, n_iterations + 1):
        # UCB1 selection
        if t <…
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.