Reference library

Python Code Samples

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

3 matches
Reliability & rate limiting easy

How to Implement a Sliding Window Counter in Python

This code implements an approximate sliding window counter using a deque of time-based buckets to track event counts within a recent time window.

sliding-window rate-limiting deque
Python
from collections import deque
from time import time


class SlidingWindowCounter:
    def __init__(self, window_size, bucket_size=1):
        self.window_size = window_size
        self.bucket_size = bucket_size
        self.buckets = deque()

    def _evict_expired(self, now):
        while self.buckets and self.buck…
13 0 Open
A/B testing & experimentation medium

How to Compute Mann-Whitney U Test in Python

Compute the Mann-Whitney U statistic and p-value manually in Python with tie correction and a normal approximation for independent samples.

statistics hypothesis-testing ab-testing
Python
import numpy as np
from scipy import stats

def mann_whitney_u_mock(sample_a, sample_b):
    """Compute Mann-Whitney U and p-value manually."""
    # Combine and rank
    combined = sample_a + sample_b
    n_a, n_b = len(sample_a), len(sample_b)
    n_total = n_a + n_b
    
    # Rank with ties handling (average ranks…
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

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.