Python Code
Samples
Copy-ready Python snippets by topic and difficulty — short, focused, and runnable in the browser editor.
How to Highlight Search Terms in Python Text
Highlights all case-insensitive occurrences of a search term in a string by wrapping them in markers.
def highlight_search_term(text: str, term: str) -> str:
"""Highlight all occurrences of term in text using terminal-style markers."""
if not term:
return text
term_lower = term.lower()
result = []
i = 0
while i < len(text):
# Check if the term starts at position i (case-insens…
Set Matrix Zeroes in Python: Markers List Grid Demo
Given a matrix, this code finds all rows and columns that contain a zero and sets every element in those rows and columns to zero, using boolean marker arrays.
def set_zeroes(matrix):
rows, cols = len(matrix), len(matrix[0])
row_markers = [False] * rows
col_markers = [False] * cols
# First pass: record which rows and columns contain zeros
for i in range(rows):
for j in range(cols):
if matrix[i][j] == 0:
row_markers[i] …
Detect Merge Conflict Markers in a File with Python
Scan a file line by line to detect Git merge conflict markers (<<<<<<<, =======, >>>>>>>) and report their line numbers with context.
from pathlib import Path
def detect_merge_conflicts(file_path):
conflicts = []
with open(file_path, 'r') as f:
lines = f.readlines()
for i, line in enumerate(lines, 1):
if line.startswith('<<<<<<<'):
conflict_marker = 'conflict start'
conflicts.append((i, confl…
pytest mark slow skip integration
Uses pytest markers to select fast tests, skip unfinished ones, and run integration checks with verbose output.
import pytest
def test_fast():
assert 1 + 1 == 2
@pytest.mark.slow
def test_slow():
import time
time.sleep(1)
assert 5 * 5 == 25
@pytest.mark.skip(reason="Not ready for production")
def test_skipped():
assert 2 + 2 == 5
@pytest.mark.integration
def test_integration():
database = {"users": […
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
- Pick a topic section — strings, lists, files, functions, and more
- Open a sample, read How it works, and copy the code block
- 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.