From 9849ef048dcac19e443d870f0afe8712adc64536 Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 27 Nov 2026 13:34:48 -0300 Subject: [PATCH] gh-65058: Document the grouping of conditional expressions (GH-162167) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The "Evaluation order" section said that expressions are always evaluated from left to right, but a conditional expression evaluates its condition first. Add a sentence and an example showing that chained conditional expressions group from right to left. (cherry picked from commit 08784b235de217bd2d63ceee008c5762a5e9b67d) Co-authored-by: Lucas Co-authored-by: Sam Joshva Co-authored-by: Bartosz Sławecki --- Doc/reference/expressions.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 733932066ca904..3c3bc08a4f5a13 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -2426,6 +2426,15 @@ The expression ``x if C else y`` first evaluates the condition, *C* rather than If *C* is true, *x* is evaluated and its value is returned; otherwise, *y* is evaluated and its value is returned. +Conditional expressions group from right to left, so +``a if b else c if d else e`` means ``a if b else (c if d else e)``:: + + >>> n = 2 + >>> print('positive' if n > 0 + ... else 'negative' if n < 0 + ... else 'zero') + positive + See :pep:`308` for more details about conditional expressions.