Reference library

Errors & debugging

Handle failures gracefully, raise helpful errors, and debug with confidence.

2 matches
Errors & debugging easy

How to Debug Print Behind a DEBUG Environment Flag in Python

Create a debug_print function that only outputs when the DEBUG environment variable is set to a truthy value like 1, true, yes, or on.

debugging environment-variables logging
Python
import os


def debug_print(*args, **kwargs):
    """Print only when DEBUG environment variable is set to a truthy value."""
    if os.environ.get("DEBUG", "").lower() in ("1", "true", "yes", "on"):
        print(*args, **kwargs)


if __name__ == "__main__":
    # Example usage: run as `DEBUG=1 python script.py` to se…
11 0 Open
Errors & debugging easy

Use pprint for Nested Structure Debug Output in Python

Pretty-print nested dictionaries and lists with pprint for readable, organized debug output.

pprint debugging nested-structure
Python
from pprint import pprint

def build_nested_structure():
    """Create a sample nested data structure for demonstration."""
    return {
        "project": "DataPipeline",
        "config": {
            "inputs": ["raw_1.json", "raw_2.json"],
            "processing": {
                "steps": ["clean", "transform",…
15 0 Open

Browse by section

Each section groups closely related Python snippets.

Errors & debugging — Python code examples

What you will find here

This page collects errors & debugging snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

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.