From 92980512cc597b0c0df51b0292ca713799fa6222 Mon Sep 17 00:00:00 2001 From: mas192 Date: Sat, 18 May 2024 22:30:34 -0700 Subject: [PATCH 1/3] revised conditonal expression with additional context and examples for right to left precedence --- Doc/reference/expressions.rst | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 00b57effd3e..e84b90c09c0 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -1905,8 +1905,23 @@ Evaluation order .. index:: pair: evaluation; order -Python evaluates expressions from left to right. Notice that while evaluating -an assignment, the right-hand side is evaluated before the left-hand side. +Python evaluates expressions from left to right (except for conditional_expression). +Notice that while evaluating an assignment, the right-hand side is evaluated before +the left-hand side. + +Examples for left to right: + + .. code-block:: python + + 2 + 3 * 4 / 2 - 1 + 7 + +Following is an example of right to left conditional_expression evaluation example: + + .. code-block:: python + + 'foo' if 1 else 'bar' if 0 else 'baz' + ' foo' In the following lines, expressions will be evaluated in the arithmetic order of their suffixes:: @@ -1930,8 +1945,11 @@ Operator precedence The following table summarizes the operator precedence in Python, from highest precedence (most binding) to lowest precedence (least binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, -operators are binary. Operators in the same box group left to right (except for -exponentiation and conditional expressions, which group from right to left). +operators are binary. Operators in the same box group left to right (except for +comparisons, including tests, exponentiation and conditional expressions, which +group from right to left which all have the same precedence and chain from left +to right — see section Comparisons — and exponentiation, which groups from right +to left) Note that comparisons, membership tests, and identity tests, all have the same precedence and have a left-to-right chaining feature as described in the From a285684da54eb4dd4d0d53baaa7b7e9133a0cd6e Mon Sep 17 00:00:00 2001 From: mas192 Date: Sat, 18 May 2024 22:50:01 -0700 Subject: [PATCH 2/3] Fix trailing whitespace in expressions.rst --- Doc/reference/expressions.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index e84b90c09c0..bf748ecafd0 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -1906,7 +1906,7 @@ Evaluation order .. index:: pair: evaluation; order Python evaluates expressions from left to right (except for conditional_expression). -Notice that while evaluating an assignment, the right-hand side is evaluated before +Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side. Examples for left to right: From 8beb707ef1d64e6f5fc5ecfaa035566e9e143c9e Mon Sep 17 00:00:00 2001 From: mas192 Date: Sat, 18 May 2024 23:04:10 -0700 Subject: [PATCH 3/3] Revised the examples for evaluation order to ensure consistent formatting. --- Doc/reference/expressions.rst | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index bf748ecafd0..af8908ee711 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -1921,17 +1921,19 @@ Following is an example of right to left conditional_expression evaluation examp .. code-block:: python 'foo' if 1 else 'bar' if 0 else 'baz' - ' foo' + 'foo' In the following lines, expressions will be evaluated in the arithmetic order of -their suffixes:: +their suffixes: - expr1, expr2, expr3, expr4 - (expr1, expr2, expr3, expr4) - {expr1: expr2, expr3: expr4} - expr1 + expr2 * (expr3 - expr4) - expr1(expr2, expr3, *expr4, **expr5) - expr3, expr4 = expr1, expr2 + .. code-block:: python + + expr1, expr2, expr3, expr4 + (expr1, expr2, expr3, expr4) + {expr1: expr2, expr3: expr4} + expr1 + expr2 * (expr3 - expr4) + expr1(expr2, expr3, *expr4, **expr5) + expr3, expr4 = expr1, expr2 .. _operator-summary: