This is a static archive of the Python wiki, which was retired in February 2026 due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.

No Dangling Else Trap

One additional benefit of using indentation is that the “dangling else ambiguity” is impossible in Python. For example, here is some C++ code:

The code sets z to 1 if both x and y are greater than 0, and it looks like it will set z to 5 if x is less than or equal to 0. But in fact, it sets z to 5 only if x is greater than 0 and if y is less than or equal to 0. Here is what it means in Python:

And if we really want z set to 5 if x is less than or equal to 0, we would write this:

Thanks to Python’s indentation-based block structure, we avoid the “dangling else” trap.


2026-02-14 16:12