input must conform to the following grammar after leading and trailing
whitespace characters are removed:
- .. productionlist::
+ .. productionlist:: float
sign: "+" | "-"
infinity: "Infinity" | "inf"
nan: "nan"
The grammar for a replacement field is as follows:
- .. productionlist:: sf
+ .. productionlist:: format-string
replacement_field: "{" [`field_name`] ["!" `conversion`] [":" `format_spec`] "}"
field_name: arg_name ("." `attribute_name` | "[" `element_index` "]")*
arg_name: [`identifier` | `digit`+]
The general form of a *standard format specifier* is:
-.. productionlist::
+.. productionlist:: format-spec
format_spec: [[`fill`]`align`][`sign`][#][0][`width`][`grouping_option`][.`precision`][`type`]
fill: <any character>
align: "<" | ">" | "=" | "^"
Summarizing:
-.. productionlist::
+
+.. productionlist:: python-grammar
compound_stmt: `if_stmt`
: | `while_stmt`
: | `for_stmt`
The :keyword:`if` statement is used for conditional execution:
-.. productionlist::
+.. productionlist:: python-grammar
if_stmt: "if" `assignment_expression` ":" `suite`
: ("elif" `assignment_expression` ":" `suite`)*
: ["else" ":" `suite`]
The :keyword:`while` statement is used for repeated execution as long as an
expression is true:
-.. productionlist::
+.. productionlist:: python-grammar
while_stmt: "while" `assignment_expression` ":" `suite`
: ["else" ":" `suite`]
The :keyword:`for` statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object:
-.. productionlist::
+.. productionlist:: python-grammar
for_stmt: "for" `target_list` "in" `expression_list` ":" `suite`
: ["else" ":" `suite`]
The :keyword:`try` statement specifies exception handlers and/or cleanup code
for a group of statements:
-.. productionlist::
+.. productionlist:: python-grammar
try_stmt: `try1_stmt` | `try2_stmt`
try1_stmt: "try" ":" `suite`
: ("except" [`expression` ["as" `identifier`]] ":" `suite`)+
This allows common :keyword:`try`...\ :keyword:`except`...\ :keyword:`finally`
usage patterns to be encapsulated for convenient reuse.
-.. productionlist::
+.. productionlist:: python-grammar
with_stmt: "with" `with_item` ("," `with_item`)* ":" `suite`
with_item: `expression` ["as" `target`]
A function definition defines a user-defined function object (see section
:ref:`types`):
-.. productionlist::
+.. productionlist:: python-grammar
funcdef: [`decorators`] "def" `funcname` "(" [`parameter_list`] ")"
: ["->" `expression`] ":" `suite`
decorators: `decorator`+
A class definition defines a class object (see section :ref:`types`):
-.. productionlist::
+.. productionlist:: python-grammar
classdef: [`decorators`] "class" `classname` [`inheritance`] ":" `suite`
inheritance: "(" [`argument_list`] ")"
classname: `identifier`
Coroutine function definition
-----------------------------
-.. productionlist::
+.. productionlist:: python-grammar
async_funcdef: [`decorators`] "async" "def" `funcname` "(" [`parameter_list`] ")"
: ["->" `expression`] ":" `suite`
The :keyword:`!async for` statement
-----------------------------------
-.. productionlist::
+.. productionlist:: python-grammar
async_for_stmt: "async" `for_stmt`
An :term:`asynchronous iterable` is able to call asynchronous code in its
The :keyword:`!async with` statement
------------------------------------
-.. productionlist::
+.. productionlist:: python-grammar
async_with_stmt: "async" `with_stmt`
An :term:`asynchronous context manager` is a :term:`context manager` that is
be used to describe syntax, not lexical analysis. When (one alternative of) a
syntax rule has the form
-.. productionlist:: *
+.. productionlist:: python-grammar
name: `othername`
and no semantics are given, the semantics of this form of ``name`` are the same
identifiers or literals. Forms enclosed in parentheses, brackets or braces are
also categorized syntactically as atoms. The syntax for atoms is:
-.. productionlist::
+.. productionlist:: python-grammar
atom: `identifier` | `literal` | `enclosure`
enclosure: `parenth_form` | `list_display` | `dict_display` | `set_display`
: | `generator_expression` | `yield_atom`
Python supports string and bytes literals and various numeric literals:
-.. productionlist::
+.. productionlist:: python-grammar
literal: `stringliteral` | `bytesliteral`
: | `integer` | `floatnumber` | `imagnumber`
A parenthesized form is an optional expression list enclosed in parentheses:
-.. productionlist::
+.. productionlist:: python-grammar
parenth_form: "(" [`starred_expression`] ")"
A parenthesized expression list yields whatever that expression list yields: if
Common syntax elements for comprehensions are:
-.. productionlist::
+.. productionlist:: python-grammar
comprehension: `assignment_expression` `comp_for`
comp_for: ["async"] "for" `target_list` "in" `or_test` [`comp_iter`]
comp_iter: `comp_for` | `comp_if`
A list display is a possibly empty series of expressions enclosed in square
brackets:
-.. productionlist::
+.. productionlist:: python-grammar
list_display: "[" [`starred_list` | `comprehension`] "]"
A list display yields a new list object, the contents being specified by either
A set display is denoted by curly braces and distinguishable from dictionary
displays by the lack of colons separating keys and values:
-.. productionlist::
+.. productionlist:: python-grammar
set_display: "{" (`starred_list` | `comprehension`) "}"
A set display yields a new mutable set object, the contents being specified by
A dictionary display is a possibly empty series of key/datum pairs enclosed in
curly braces:
-.. productionlist::
+.. productionlist:: python-grammar
dict_display: "{" [`key_datum_list` | `dict_comprehension`] "}"
key_datum_list: `key_datum` ("," `key_datum`)* [","]
key_datum: `expression` ":" `expression` | "**" `or_expr`
A generator expression is a compact generator notation in parentheses:
-.. productionlist::
+.. productionlist:: python-grammar
generator_expression: "(" `expression` `comp_for` ")"
A generator expression yields a new generator object. Its syntax is the same as
pair: yield; expression
pair: generator; function
-.. productionlist::
+.. productionlist:: python-grammar
yield_atom: "(" `yield_expression` ")"
yield_expression: "yield" [`expression_list` | "from" `expression`]
Primaries represent the most tightly bound operations of the language. Their
syntax is:
-.. productionlist::
+.. productionlist:: python-grammar
primary: `atom` | `attributeref` | `subscription` | `slicing` | `call`
An attribute reference is a primary followed by a period and a name:
-.. productionlist::
+.. productionlist:: python-grammar
attributeref: `primary` "." `identifier`
.. index::
A subscription selects an item of a sequence (string, tuple or list) or mapping
(dictionary) object:
-.. productionlist::
+.. productionlist:: python-grammar
subscription: `primary` "[" `expression_list` "]"
The primary must evaluate to an object that supports subscription (lists or
or list). Slicings may be used as expressions or as targets in assignment or
:keyword:`del` statements. The syntax for a slicing:
-.. productionlist::
+.. productionlist:: python-grammar
slicing: `primary` "[" `slice_list` "]"
slice_list: `slice_item` ("," `slice_item`)* [","]
slice_item: `expression` | `proper_slice`
A call calls a callable object (e.g., a :term:`function`) with a possibly empty
series of :term:`arguments <argument>`:
-.. productionlist::
+.. productionlist:: python-grammar
call: `primary` "(" [`argument_list` [","] | `comprehension`] ")"
argument_list: `positional_arguments` ["," `starred_and_keywords`]
: ["," `keywords_arguments`]
Suspend the execution of :term:`coroutine` on an :term:`awaitable` object.
Can only be used inside a :term:`coroutine function`.
-.. productionlist::
+.. productionlist:: python-grammar
await_expr: "await" `primary`
.. versionadded:: 3.5
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:
-.. productionlist::
+.. productionlist:: python-grammar
power: (`await_expr` | `primary`) ["**" `u_expr`]
Thus, in an unparenthesized sequence of power and unary operators, the operators
All unary arithmetic and bitwise operations have the same priority:
-.. productionlist::
+.. productionlist:: python-grammar
u_expr: `power` | "-" `u_expr` | "+" `u_expr` | "~" `u_expr`
.. index::
from the power operator, there are only two levels, one for multiplicative
operators and one for additive operators:
-.. productionlist::
+.. productionlist:: python-grammar
m_expr: `u_expr` | `m_expr` "*" `u_expr` | `m_expr` "@" `m_expr` |
: `m_expr` "//" `u_expr` | `m_expr` "/" `u_expr` |
: `m_expr` "%" `u_expr`
The shifting operations have lower priority than the arithmetic operations:
-.. productionlist::
+.. productionlist:: python-grammar
shift_expr: `a_expr` | `shift_expr` ("<<" | ">>") `a_expr`
These operators accept integers as arguments. They shift the first argument to
Each of the three bitwise operations has a different priority level:
-.. productionlist::
+.. productionlist:: python-grammar
and_expr: `shift_expr` | `and_expr` "&" `shift_expr`
xor_expr: `and_expr` | `xor_expr` "^" `and_expr`
or_expr: `xor_expr` | `or_expr` "|" `xor_expr`
C, expressions like ``a < b < c`` have the interpretation that is conventional
in mathematics:
-.. productionlist::
+.. productionlist:: python-grammar
comparison: `or_expr` (`comp_operator` `or_expr`)*
comp_operator: "<" | ">" | "==" | ">=" | "<=" | "!="
: | "is" ["not"] | ["not"] "in"
pair: Conditional; expression
pair: Boolean; operation
-.. productionlist::
+.. productionlist:: python-grammar
or_test: `and_test` | `or_test` "or" `and_test`
and_test: `not_test` | `and_test` "and" `not_test`
not_test: `comparison` | "not" `not_test`
Assignment expressions
======================
-.. productionlist::
+.. productionlist:: python-grammar
assignment_expression: [`identifier` ":="] `expression`
An assignment expression (sometimes also called a "named expression" or
single: if; conditional expression
single: else; conditional expression
-.. productionlist::
+.. productionlist:: python-grammar
conditional_expression: `or_test` ["if" `or_test` "else" `expression`]
expression: `conditional_expression` | `lambda_expr`
expression_nocond: `or_test` | `lambda_expr_nocond`
pair: anonymous; function
single: : (colon); lambda expression
-.. productionlist::
+.. productionlist:: python-grammar
lambda_expr: "lambda" [`parameter_list`] ":" `expression`
lambda_expr_nocond: "lambda" [`parameter_list`] ":" `expression_nocond`
pair: expression; list
single: , (comma); expression list
-.. productionlist::
+.. productionlist:: python-grammar
expression_list: `expression` ("," `expression`)* [","]
starred_list: `starred_item` ("," `starred_item`)* [","]
starred_expression: `expression` | (`starred_item` ",")* [`starred_item`]
The descriptions of lexical analysis and syntax use a modified BNF grammar
notation. This uses the following style of definition:
-.. productionlist::
+.. productionlist:: notation
name: `lc_letter` (`lc_letter` | "_")*
lc_letter: "a"..."z"
Identifiers are unlimited in length. Case is significant.
-.. productionlist::
+.. productionlist:: python-grammar
identifier: `xid_start` `xid_continue`*
id_start: <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property>
id_continue: <all characters in `id_start`, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property>
String literals are described by the following lexical definitions:
-.. productionlist::
+.. productionlist:: python-grammar
stringliteral: [`stringprefix`](`shortstring` | `longstring`)
stringprefix: "r" | "u" | "R" | "U" | "f" | "F"
: | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | "Rf" | "RF"
longstringchar: <any source character except "\">
stringescapeseq: "\" <any source character>
-.. productionlist::
+.. productionlist:: python-grammar
bytesliteral: `bytesprefix`(`shortbytes` | `longbytes`)
bytesprefix: "b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB"
shortbytes: "'" `shortbytesitem`* "'" | '"' `shortbytesitem`* '"'
a literal is also marked as a raw string). After decoding, the grammar
for the contents of the string is:
-.. productionlist::
+.. productionlist:: python-grammar
f_string: (`literal_char` | "{{" | "}}" | `replacement_field`)*
replacement_field: "{" `f_expression` ["="] ["!" `conversion`] [":" `format_spec`] "}"
f_expression: (`conditional_expression` | "*" `or_expr`)
Integer literals are described by the following lexical definitions:
-.. productionlist::
+.. productionlist:: python-grammar
integer: `decinteger` | `bininteger` | `octinteger` | `hexinteger`
decinteger: `nonzerodigit` (["_"] `digit`)* | "0"+ (["_"] "0")*
bininteger: "0" ("b" | "B") (["_"] `bindigit`)+
Floating point literals are described by the following lexical definitions:
-.. productionlist::
+.. productionlist:: python-grammar
floatnumber: `pointfloat` | `exponentfloat`
pointfloat: [`digitpart`] `fraction` | `digitpart` "."
exponentfloat: (`digitpart` | `pointfloat`) `exponent`
Imaginary literals are described by the following lexical definitions:
-.. productionlist::
+.. productionlist:: python-grammar
imagnumber: (`floatnumber` | `digitpart`) ("j" | "J")
An imaginary literal yields a complex number with a real part of 0.0. Complex
statements may occur on a single line separated by semicolons. The syntax for
simple statements is:
-.. productionlist::
+.. productionlist:: python-grammar
simple_stmt: `expression_stmt`
: | `assert_stmt`
: | `assignment_stmt`
expression statements are allowed and occasionally useful. The syntax for an
expression statement is:
-.. productionlist::
+.. productionlist:: python-grammar
expression_stmt: `starred_expression`
An expression statement evaluates the expression list (which may be a single
Assignment statements are used to (re)bind names to values and to modify
attributes or items of mutable objects:
-.. productionlist::
+.. productionlist:: python-grammar
assignment_stmt: (`target_list` "=")+ (`starred_expression` | `yield_expression`)
target_list: `target` ("," `target`)* [","]
target: `identifier`
Augmented assignment is the combination, in a single statement, of a binary
operation and an assignment statement:
-.. productionlist::
+.. productionlist:: python-grammar
augmented_assignment_stmt: `augtarget` `augop` (`expression_list` | `yield_expression`)
augtarget: `identifier` | `attributeref` | `subscription` | `slicing`
augop: "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**="
:term:`Annotation <variable annotation>` assignment is the combination, in a single
statement, of a variable or attribute annotation and an optional assignment statement:
-.. productionlist::
+.. productionlist:: python-grammar
annotated_assignment_stmt: `augtarget` ":" `expression`
: ["=" (`starred_expression` | `yield_expression`)]
Assert statements are a convenient way to insert debugging assertions into a
program:
-.. productionlist::
+.. productionlist:: python-grammar
assert_stmt: "assert" `expression` ["," `expression`]
The simple form, ``assert expression``, is equivalent to ::
pair: null; operation
pair: null; operation
-.. productionlist::
+.. productionlist:: python-grammar
pass_stmt: "pass"
:keyword:`pass` is a null operation --- when it is executed, nothing happens.
pair: deletion; target
triple: deletion; target; list
-.. productionlist::
+.. productionlist:: python-grammar
del_stmt: "del" `target_list`
Deletion is recursively defined very similar to the way assignment is defined.
pair: function; definition
pair: class; definition
-.. productionlist::
+.. productionlist:: python-grammar
return_stmt: "return" [`expression_list`]
:keyword:`return` may only occur syntactically nested in a function definition,
single: function; generator
exception: StopIteration
-.. productionlist::
+.. productionlist:: python-grammar
yield_stmt: `yield_expression`
A :keyword:`yield` statement is semantically equivalent to a :ref:`yield
pair: raising; exception
single: __traceback__ (exception attribute)
-.. productionlist::
+.. productionlist:: python-grammar
raise_stmt: "raise" [`expression` ["from" `expression`]]
If no expressions are present, :keyword:`raise` re-raises the last exception
statement: while
pair: loop; statement
-.. productionlist::
+.. productionlist:: python-grammar
break_stmt: "break"
:keyword:`break` may only occur syntactically nested in a :keyword:`for` or
pair: loop; statement
keyword: finally
-.. productionlist::
+.. productionlist:: python-grammar
continue_stmt: "continue"
:keyword:`continue` may only occur syntactically nested in a :keyword:`for` or
exception: ImportError
single: , (comma); import statement
-.. productionlist::
+.. productionlist:: python-grammar
import_stmt: "import" `module` ["as" `identifier`] ("," `module` ["as" `identifier`])*
: | "from" `relative_module` "import" `identifier` ["as" `identifier`]
: ("," `identifier` ["as" `identifier`])*
features on a per-module basis before the release in which the feature becomes
standard.
-.. productionlist:: *
+.. productionlist:: python-grammar
future_stmt: "from" "__future__" "import" `feature` ["as" `identifier`]
: ("," `feature` ["as" `identifier`])*
: | "from" "__future__" "import" "(" `feature` ["as" `identifier`]
triple: global; name; binding
single: , (comma); identifier list
-.. productionlist::
+.. productionlist:: python-grammar
global_stmt: "global" `identifier` ("," `identifier`)*
The :keyword:`global` statement is a declaration which holds for the entire
.. index:: statement: nonlocal
single: , (comma); identifier list
-.. productionlist::
+.. productionlist:: python-grammar
nonlocal_stmt: "nonlocal" `identifier` ("," `identifier`)*
.. XXX add when implemented
All input read from non-interactive files has the same form:
-.. productionlist::
+.. productionlist:: python-grammar
file_input: (NEWLINE | `statement`)*
This syntax is used in the following situations:
Input in interactive mode is parsed using the following grammar:
-.. productionlist::
+.. productionlist:: python-grammar
interactive_input: [`stmt_list`] NEWLINE | `compound_stmt` NEWLINE
Note that a (top-level) compound statement must be followed by a blank line in
:func:`eval` is used for expression input. It ignores leading whitespace. The
string argument to :func:`eval` must have the following form:
-.. productionlist::
+.. productionlist:: python-grammar
eval_input: `expression_list` NEWLINE*