Reference library
Python code samples
Medium snippets you can copy, study, and run in the browser editor.
Convert Image to ASCII Art in Python
Convert any image to ASCII art by resizing, converting to grayscale, and mapping pixel brightness to characters using Pillow.
from PIL import Image
import sys
ASCII_CHARS = "@%#*+=-:. "
def resize_image(image, new_width=100):
"""Resize image maintaining aspect ratio."""
width, height = image.size
ratio = height / width
new_height = int(new_width * ratio * 0.55) # 0.55 adjusts for font aspect ratio
return image.resize((…
How to Generate Beautiful QR Codes with Embedded Logos in Python
Generate a high-error-correction QR code and paste a logo image in the center to create a branded, scannable QR code.
import qrcode
from PIL import Image
def generate_qr_with_logo(data, logo_path, output_path):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
qr_img = qr.make_image(fill_c…
Detect and Remove Blurry Images in Python with OpenCV
Automatically scan a directory of images, detect blur using Laplacian variance, and remove blurry images with a dry-run option for safety.
from pathlib import Path
import cv2
import numpy as np
def is_blurry(image_path, threshold=100.0):
"""
Detect if an image is blurry using Laplacian variance.
Returns True if blurry, False otherwise.
"""
img = cv2.imread(str(image_path), cv2.IMREAD_GRAYSCALE)
if img is None:
return True…
Download Images from a Web Page Automatically in Python
Scrape all images from a webpage, filter by extension, and save them to a local folder using requests and BeautifulSoup.
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import os
def download_images(url, output_folder="downloaded_images"):
"""Download all images from a given URL."""
os.makedirs(output_folder, exist_ok=True)
response = requests.get(url)
response.raise_for_status()
…
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.