Reference library

Python Code Samples

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

27 matches
Big data & Spark easy

Sliding Window Streaming Mock in Python

A simple Python class that maintains a sliding window of recent streaming values and computes the running average.

streaming sliding-window averages
Python
import time
import random

class StreamingMock:
    """Produces a stream of numbers using a sliding window."""
    
    def __init__(self, window_size=5):
        self.window = []
        self.window_size = window_size
        
    def push(self, value):
        """Add a value, sliding the window forward."""
        s…
12 0 Open
A/B testing & experimentation easy

How to Calculate Weighted Grades and Generate Mock Notes in Python

Compute a weighted physics grade from exam and homework scores, then generate a performance-based mock note with percentage and feedback.

grades weighted-average mock-note
Python
def get_physics_grade(exam_score, homework_score):
    """Calculate final grade from exam and homework scores."""
    exam_weight = 0.7
    homework_weight = 0.3
    return (exam_score * exam_weight) + (homework_score * homework_weight)


def mock_note(correct_score, max_score, student_name):
    """Generate a mock no…
10 0 Open
Production deployment patterns easy

How to Build a Synthetic Monitor Mock in Python

Simulates a synthetic monitoring system in Python that collects latency samples, averages them, and reports service status as UP or DEGRADED.

monitoring dataclass simulation
Python
import random
import time
from dataclasses import dataclass, field
from statistics import mean


@dataclass
class SyntheticMonitor:
    service: str
    endpoint: str
    latency_ms: list[float] = field(default_factory=list)

    def check(self) -> float:
        latency = random.uniform(50.0, 250.0)
        self.late…
13 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.