Reference library

Python Code Samples

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

4 matches
Files & data medium

How to Compare Two Files by Content Hash Equality in Python

Compares two files by hashing their contents with SHA-256, skipping the hash if file sizes differ, and returns whether they are identical.

hashlib sha256 file-hashing
Python
import hashlib
from pathlib import Path

def file_hash(path: Path, chunk_size: int = 8192) -> str:
    sha256 = hashlib.sha256()
    with path.open("rb") as f:
        for chunk in iter(lambda: f.read(chunk_size), b""):
            sha256.update(chunk)
    return sha256.hexdigest()

def files_are_identical(file_a: Pat…
13 0 Open
Algorithms & data structures medium

How to Find Four Sum Quadruplets in Python (Sorted Demo)

Find all unique quadruplets in a sorted array that sum to a target, with duplicate skipping.

two-pointers sorting four-sum
Python
def four_sum(nums, target):
    nums.sort()
    result = []
    n = len(nums)

    for i in range(n - 3):
        if i > 0 and nums[i] == nums[i - 1]:
            continue
        for j in range(i + 1, n - 2):
            if j > i + 1 and nums[j] == nums[j - 1]:
                continue
            left, right = j + 1…
13 0 Open
Git + Python medium

How to Archive a Repository as a ZIP in Python

Create a ZIP archive of a repository directory with a mock export, skipping hidden files and __pycache__ folders.

zipfile os.walk archiving
Python
import zipfile
import io
import os
from pathlib import Path


def archive_repo_mock(repo_path, output_path="repo_archive.zip"):
    """Create a zip archive of a repository directory (mock export)."""
    repo = Path(repo_path)
    if not repo.exists():
        raise FileNotFoundError(f"Repository not found: {repo}")

…
13 0 Open
Concurrency & performance medium

How to Reduce Instance Memory with __slots__ in Python

Demonstrates that classes with __slots__ use less memory per instance than regular classes because they skip the instance __dict__.

__slots__ memory performance
Python
class SlottedPoint:
    __slots__ = ('x', 'y', 'z')

    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z


class RegularPoint:
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z


if __name__ == "__main__":
    regular = RegularPoint(1, 2, 3)…
11 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.