Reference library

Python Code Samples

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

5 matches
Automation & scripting medium

Build an RSS feed from markdown blog posts in Python

Scans a folder of markdown files, extracts titles, dates, and excerpts, and generates a valid RSS 2.0 XML feed.

rss markdown xml
Python
import re
from pathlib import Path
from xml.etree.ElementTree import Element, SubElement, tostring
from datetime import datetime, timezone
from xml.dom import minidom

def build_rss(blog_dir, site_url="https://example.com"):
    feed = Element("rss", version="2.0")
    channel = SubElement(feed, "channel")
    SubElem…
11 0 Open
Automation & scripting medium

Generate Beautiful Project Documentation from Python Source Code Automatically

Automatically generate a markdown summary of function docstrings from any Python source file using the AST module.

ast automation documentation
Python
import ast
import inspect
from pathlib import Path

def extract_docstrings_from_file(filepath):
    """Parse a Python file and collect function docstrings."""
    source = Path(filepath).read_text()
    tree = ast.parse(source)

    docs = []
    for node in ast.walk(tree):
        if isinstance(node, (ast.FunctionDef…
34 0 Open
Git + Python medium

Generate CHANGELOG from Conventional Commits in Python

Parse your git log for conventional commits (feat, fix) and produce a simple Markdown CHANGELOG with grouped features and bug fixes.

git changelog automation
Python
import subprocess
import re
import sys
from collections import OrderedDict

CONVENTIONAL_COMMIT = re.compile(
    r"^(?P<type>feat|fix|chore|docs|refactor|perf|test|build|ci|style)(?:\((?P<scope>[^)]+)\))?: (?P<description>.+)"
)


def get_git_log():
    return subprocess.run(
        ["git", "log", "--format=%s"],
  …
14 0 Open
Git + Python medium

Generate Release Notes Markdown from PR Titles in Python

Generate structured Markdown release notes from a list of pull request titles using conventional commit types.

release-notes git pr-titles
Python
import json
from datetime import datetime, timezone

PRS = [
    {"title": "feat: add user login", "number": 12, "merged_at": "2025-01-10"},
    {"title": "fix: resolve payment timeout", "number": 13, "merged_at": "2025-01-11"},
    {"title": "chore: bump dependencies", "number": 14, "merged_at": "2025-01-12"},
    {"…
15 0 Open
Modern tooling medium

How to Mock a semantic-release Changelog in Python

This Python code simulates a semantic-release changelog generator, grouping commits by type and formatting them into a markdown changelog.

semantic-release changelog automation
Python
import json
from datetime import datetime


class SemanticReleaseChangelog:
    def __init__(self, version, commits):
        self.version = version
        self.commits = commits
        self.release_date = datetime.now().isoformat()

    def generate_changelog(self):
        grouped = {}
        for commit in self.c…
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.