Real Python
  • Start Here
  • Learn Python
    Python Tutorials →
    In-depth articles and video courses
    Learning Paths →
    Guided study plans for accelerated learning
    Quizzes & Exercises →
    Check your learning progress
    Browse Topics →
    Focus on a specific area or skill level
    Community Chat →
    Learn with other Pythonistas
    Office Hours →
    Live Q&A calls with Python experts
    Podcast →
    Hear what’s new in the world of Python
    Books →
    Round out your knowledge and learn offline
    Reference →
    Concise definitions for common Python terms
    Code Mentor →Beta
    Personalized code assistance & learning tools
    Unlock All Content →
  • More
    Learner Stories Python Newsletter Python Job Board Meet the Team Become a Tutorial Writer Become a Video Instructor
  • Search
  • Join
  • Sign‑In
Browse Topics Guided Learning Paths
Basics Intermediate Advanced
ai algorithms api best-practices career community databases data-science data-structures data-viz devops django docker editors flask front-end gamedev gui machine-learning news numpy projects python stdlib testing tools web-dev web-scraping

Table of Contents

  • Getting to Know Assertions in Python
    • What Are Assertions?
    • What Are Assertions Good For?
    • When Not to Use Assertions?
  • Understanding Python’s assert Statements
    • The Syntax of the assert Statement
    • The AssertionError Exception
  • Exploring Common Assertion Formats
  • Documenting Your Code With Assertions
  • Debugging Your Code With Assertions
    • An Example of Debugging With Assertions
    • A Few Considerations on Debugging With Assertions
  • Disabling Assertions in Production for Performance
    • Understanding the __debug__ Built-in Constant
    • Running Python With the -O or -OO Options
    • Setting the PYTHONOPTIMIZE Environment Variable
    • Running Python in Optimized Mode
  • Testing Your Code With Assertions
  • Understanding Common Pitfalls of assert
    • Using assert for Data Processing and Validation
    • Handling Errors With assert
    • Running assert on Expressions With Side Effects
    • Impacting Performance With assert
    • Having assert Statements Enabled by Default
  • Conclusion
  • Frequently Asked Questions

Recommended Course