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.

Contents

The Problem

Python is sorely missing an if statement that:

In C and its derivitives,

predicate ? A : B 

This is extremely useful in programming and helps avoid copy and paste assignments. This is an example of poor code, because the assignment logic "x=" is copied and pasted, violating the software maintainbility and readability principle of OnceAndOnlyOnce:

if test:
   x=sin(cos(x))
else:
   x=cos(sin(x))

Accepted Solution

This is now part of Python 2.5:

A if predicate else B

which would convert the above snippet to:

x = sin(cos(x)) if test else cos(sin(x))

Discussion

Please see http://python.org/peps/pep-0308.html and google for discussions about the ternary operator (if you haven't yet).


2026-02-14 16:09