summaryrefslogtreecommitdiffstats
path: root/Doc/reference/expressions.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/reference/expressions.rst')
-rw-r--r--Doc/reference/expressions.rst145
1 files changed, 116 insertions, 29 deletions
diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst
index f4b1618..fdbbba1 100644
--- a/Doc/reference/expressions.rst
+++ b/Doc/reference/expressions.rst
@@ -128,7 +128,10 @@ value.
Parenthesized forms
-------------------
-.. index:: single: parenthesized form
+.. index::
+ single: parenthesized form
+ single: (; tuple display
+ single: ); tuple display
A parenthesized form is an optional expression list enclosed in parentheses:
@@ -146,8 +149,9 @@ immutable, the rules for literals apply (i.e., two occurrences of the empty
tuple may or may not yield the same object).
.. index::
- single: comma
+ single: comma; tuple display
pair: tuple; display
+ single: ,; tuple display
Note that tuples are not formed by the parentheses, but rather by use of the
comma operator. The exception is the empty tuple, for which parentheses *are*
@@ -168,6 +172,11 @@ called "displays", each of them in two flavors:
* they are computed via a set of looping and filtering instructions, called a
:dfn:`comprehension`.
+.. index::
+ single: for; in comprehensions
+ single: if; in comprehensions
+ single: async for; in comprehensions
+
Common syntax elements for comprehensions are:
.. productionlist::
@@ -198,6 +207,9 @@ To ensure the comprehension always results in a container of the appropriate
type, ``yield`` and ``yield from`` expressions are prohibited in the implicitly
nested scope.
+.. index::
+ single: await; in comprehensions
+
Since Python 3.6, in an :keyword:`async def` function, an :keyword:`async for`
clause may be used to iterate over a :term:`asynchronous iterator`.
A comprehension in an :keyword:`async def` function may consist of either a
@@ -227,6 +239,9 @@ List displays
pair: list; comprehensions
pair: empty; list
object: list
+ single: [; list expression
+ single: ]; list expression
+ single: ,; expression list
A list display is a possibly empty series of expressions enclosed in square
brackets:
@@ -246,8 +261,12 @@ the list is constructed from the elements resulting from the comprehension.
Set displays
------------
-.. index:: pair: set; display
- object: set
+.. index::
+ pair: set; display
+ object: set
+ single: {; set expression
+ single: }; set expression
+ single: ,; expression list
A set display is denoted by curly braces and distinguishable from dictionary
displays by the lack of colons separating keys and values:
@@ -270,9 +289,14 @@ dictionary.
Dictionary displays
-------------------
-.. index:: pair: dictionary; display
- key, datum, key/datum pair
- object: dictionary
+.. index::
+ pair: dictionary; display
+ key, datum, key/datum pair
+ object: dictionary
+ single: {; dictionary expression
+ single: }; dictionary expression
+ single: :; in dictionary expressions
+ single: ,; in dictionary displays
A dictionary display is a possibly empty series of key/datum pairs enclosed in
curly braces:
@@ -291,7 +315,9 @@ used as a key into the dictionary to store the corresponding datum. This means
that you can specify the same key multiple times in the key/datum list, and the
final dictionary's value for that key will be the last one given.
-.. index:: unpacking; dictionary, **; in dictionary displays
+.. index::
+ unpacking; dictionary
+ single: **; in dictionary displays
A double asterisk ``**`` denotes :dfn:`dictionary unpacking`.
Its operand must be a :term:`mapping`. Each mapping item is added
@@ -321,8 +347,11 @@ prevails.
Generator expressions
---------------------
-.. index:: pair: generator; expression
- object: generator
+.. index::
+ pair: generator; expression
+ object: generator
+ single: (; generator expression
+ single: ); generator expression
A generator expression is a compact generator notation in parentheses:
@@ -376,6 +405,7 @@ Yield expressions
.. index::
keyword: yield
+ keyword: from
pair: yield; expression
pair: generator; function
@@ -439,6 +469,9 @@ finalized (by reaching a zero reference count or by being garbage collected),
the generator-iterator's :meth:`~generator.close` method will be called,
allowing any pending :keyword:`finally` clauses to execute.
+.. index::
+ single: from; yield from expression
+
When ``yield from <expr>`` is used, it treats the supplied expression as
a subiterator. All values produced by that subiterator are passed directly
to the caller of the current generator's methods. Any values passed in with
@@ -718,7 +751,9 @@ syntax is:
Attribute references
--------------------
-.. index:: pair: attribute; reference
+.. index::
+ pair: attribute; reference
+ single: .; attribute reference
An attribute reference is a primary followed by a period and a name:
@@ -744,7 +779,10 @@ same attribute reference may yield different objects.
Subscriptions
-------------
-.. index:: single: subscription
+.. index::
+ single: subscription
+ single: [; subscription
+ single: ]; subscription
.. index::
object: sequence
@@ -801,6 +839,8 @@ Slicings
.. index::
single: slicing
single: slice
+ single: :; slicing
+ single: ,; slicing
.. index::
object: sequence
@@ -850,6 +890,10 @@ substituting ``None`` for missing expressions.
object: callable
single: call
single: argument; call semantics
+ single: (; call
+ single: ); call
+ single: ,; argument list
+ single: =; in function calls
.. _calls:
@@ -1032,6 +1076,7 @@ a class instance:
if that method was called.
+.. index:: keyword: await
.. _await:
Await expression
@@ -1051,6 +1096,10 @@ Can only be used inside a :term:`coroutine function`.
The power operator
==================
+.. index::
+ pair: power; operation
+ operator: **
+
The power operator binds more tightly than unary operators on its left; it binds
less tightly than unary operators on its right. The syntax is:
@@ -1093,15 +1142,21 @@ All unary arithmetic and bitwise operations have the same priority:
.. index::
single: negation
single: minus
+ single: operator; -
+ single: -; unary operator
The unary ``-`` (minus) operator yields the negation of its numeric argument.
-.. index:: single: plus
+.. index::
+ single: plus
+ single: operator; +
+ single: +; unary operator
The unary ``+`` (plus) operator yields its numeric argument unchanged.
-.. index:: single: inversion
-
+.. index::
+ single: inversion
+ operator: ~
The unary ``~`` (invert) operator yields the bitwise inversion of its integer
argument. The bitwise inversion of ``x`` is defined as ``-(x+1)``. It only
@@ -1131,7 +1186,9 @@ operators and one for additive operators:
: `m_expr` "%" `u_expr`
a_expr: `m_expr` | `a_expr` "+" `m_expr` | `a_expr` "-" `m_expr`
-.. index:: single: multiplication
+.. index::
+ single: multiplication
+ operator: *
The ``*`` (multiplication) operator yields the product of its arguments. The
arguments must either both be numbers, or one argument must be an integer and
@@ -1151,6 +1208,8 @@ builtin Python types implement this operator.
.. index::
exception: ZeroDivisionError
single: division
+ operator: /
+ operator: //
The ``/`` (division) and ``//`` (floor division) operators yield the quotient of
their arguments. The numeric arguments are first converted to a common type.
@@ -1159,7 +1218,9 @@ integer; the result is that of mathematical division with the 'floor' function
applied to the result. Division by zero raises the :exc:`ZeroDivisionError`
exception.
-.. index:: single: modulo
+.. index::
+ single: modulo
+ operator: %
The ``%`` (modulo) operator yields the remainder from the division of the first
argument by the second. The numeric arguments are first converted to a common
@@ -1184,14 +1245,20 @@ The floor division operator, the modulo operator, and the :func:`divmod`
function are not defined for complex numbers. Instead, convert to a floating
point number using the :func:`abs` function if appropriate.
-.. index:: single: addition
+.. index::
+ single: addition
+ single: operator; +
+ single: +; binary operator
The ``+`` (addition) operator yields the sum of its arguments. The arguments
must either both be numbers or both be sequences of the same type. In the
former case, the numbers are converted to a common type and then added together.
In the latter case, the sequences are concatenated.
-.. index:: single: subtraction
+.. index::
+ single: subtraction
+ single: operator; -
+ single: -; binary operator
The ``-`` (subtraction) operator yields the difference of its arguments. The
numeric arguments are first converted to a common type.
@@ -1202,7 +1269,10 @@ numeric arguments are first converted to a common type.
Shifting operations
===================
-.. index:: pair: shifting; operation
+.. index::
+ pair: shifting; operation
+ operator: <<
+ operator: >>
The shifting operations have lower priority than the arithmetic operations:
@@ -1232,7 +1302,9 @@ Each of the three bitwise operations has a different priority level:
xor_expr: `and_expr` | `xor_expr` "^" `and_expr`
or_expr: `xor_expr` | `or_expr` "|" `xor_expr`
-.. index:: pair: bitwise; and
+.. index::
+ pair: bitwise; and
+ operator: &
The ``&`` operator yields the bitwise AND of its arguments, which must be
integers.
@@ -1240,6 +1312,7 @@ integers.
.. index::
pair: bitwise; xor
pair: exclusive; or
+ operator: ^
The ``^`` operator yields the bitwise XOR (exclusive OR) of its arguments, which
must be integers.
@@ -1247,6 +1320,7 @@ must be integers.
.. index::
pair: bitwise; or
pair: inclusive; or
+ operator: |
The ``|`` operator yields the bitwise (inclusive) OR of its arguments, which
must be integers.
@@ -1257,9 +1331,15 @@ must be integers.
Comparisons
===========
-.. index:: single: comparison
-
-.. index:: pair: C; language
+.. index::
+ single: comparison
+ pair: C; language
+ operator: <
+ operator: >
+ operator: <=
+ operator: >=
+ operator: ==
+ operator: !=
Unlike C, all comparison operations in Python have the same priority, which is
lower than that of any arithmetic, shifting or bitwise operation. Also unlike
@@ -1577,6 +1657,8 @@ Conditional expressions
.. index::
pair: conditional; expression
pair: ternary; operator
+ single: if; conditional expression
+ single: else; conditional expression
.. productionlist::
conditional_expression: `or_test` ["if" `or_test` "else" `expression`]
@@ -1603,10 +1685,11 @@ Lambdas
pair: lambda; expression
pair: lambda; form
pair: anonymous; function
+ single: :; lambda expression
.. productionlist::
- lambda_expr: "lambda" [`parameter_list`]: `expression`
- lambda_expr_nocond: "lambda" [`parameter_list`]: `expression_nocond`
+ lambda_expr: "lambda" [`parameter_list`] ":" `expression`
+ lambda_expr_nocond: "lambda" [`parameter_list`] ":" `expression_nocond`
Lambda expressions (sometimes called lambda forms) are used to create anonymous
functions. The expression ``lambda parameters: expression`` yields a function
@@ -1627,7 +1710,10 @@ annotations.
Expression lists
================
-.. index:: pair: expression; list
+.. index::
+ pair: expression; list
+ single: comma; expression list
+ single: ,; expression list
.. productionlist::
expression_list: `expression` ("," `expression`)* [","]
@@ -1689,7 +1775,8 @@ their suffixes::
Operator precedence
===================
-.. index:: pair: operator; precedence
+.. index::
+ pair: operator; precedence
The following table summarizes the operator precedence in Python, from lowest
precedence (least binding) to highest precedence (most binding). Operators in
@@ -1737,7 +1824,7 @@ precedence and have a left-to-right chaining feature as described in the
+-----------------------------------------------+-------------------------------------+
| ``**`` | Exponentiation [#]_ |
+-----------------------------------------------+-------------------------------------+
-| ``await`` ``x`` | Await expression |
+| :keyword:`await` ``x`` | Await expression |
+-----------------------------------------------+-------------------------------------+
| ``x[index]``, ``x[index:index]``, | Subscription, slicing, |
| ``x(arguments...)``, ``x.attribute`` | call, attribute reference |