This page provides a semi-formal specification of the Python subset that we are currently using.
Material added for this week is highlighted in pink.
Pressing the "Toggle semantics" button removes (or reinstates) the boxes containing the semantics (meanings) of each form. Javascript must be enabled in your browser for this button to work.
A parse tree tool allows you to enter a program in this syntax and view its automatically-generated syntax tree and a random tree tool allows you to randomly generate expressions according to a subset of the Python expression syntax (and a tiny subset of English). These are valuable tools for learning to think in terms of formal syntax.
Please send comments for improvement to chaynes@indiana.edu.
Names of syntactic forms, such as program and simple_variable_assignment are in bold italic font.
Indented content following the name of a syntactic form is its definition.
References to syntactic forms are hyper-linked to their definition (and so appear in blue). Such references may have an italic prefix and/or postfix, attached by an underscore, used to indicate the semantic significance and/or type of the reference. For example, variable_name is a name that refers to a variable and test_expression indicates an expression whose value is interpreted as being true or false.
When there is more than one alternative for a syntactic form, they are separated by or.
The ellipsis notation syntactic_form,...,syntactic_form indicates a comma-separated sequence of zero or more instances
of the indicated syntactic form.Square brackets enclose optional syntax, as in the syntax of a return_statement, and square brackets followed by an ellipsis, [syntax]..., indicate that syntax may be repeated zero or more times, as in the syntax of an if_statement.
Literals characters, such as if, or the parenthesis in a parenthetic_expression, are entered in programs exactly as they appear here, are in code font.Parenthetic comments (like this) indicate additional information associated with the syntactic element on that line, such as a common name for it, or limitation on its use.
For example, one of the possibilities for a statement is a simple_variable_assignment that is composed of variable name, followed by the literal
character =, followed by an expression. You can click on name or expression and the display will jump to the definition of that syntactic
form. Unless the "Toggle semantics" button at the top has been pressed,
this is followed by a box describing the meaning (effect) of an
import statement.Any # character that is not in a string is the
beginning of a comment that extends to the end of the line, and the comment is
ignored when the line is parsed.Each line is indented the same amount as the previous line, unless indentation is increased at the beginning of an indented block, or returning to the previous level at the end of an indented blcok.
One or more spaces are required to separate keywords or names
that are adjacent to each other. Spaces are not allowed in identifiers,
keywords, or in multicharacter operators, such as
<=. Spaces also have significance in strings and
indentation. In other places, spaces, tabs, and blank lines may be
inserted as desired.Every statement begins on a new line. New lines always begin a new statement, unless theyare part of a multi-line string literal, or follow a block and continue a compound statement.
SemanticsThe semantics (meaning) of syntactic forms are indicated by boxed text, like this. Unless otherwise indicated, subexpressions (any expression indicated in the syntax of the form) are evaluated before other action is taken. It is an error if the value of an expression does not satisfy all indicated constraints. |
.py.| The statements are executed in order. |
expression
| ||
| or |
| |
| or |
| |
| or |
| |
| or |
| |
| or |
| |
| or |
| |
| or |
| |
| or |
| |
| or |
| |
| or |
| |
| or |
| |
| or |
|
a-z and A-Z), digit (0-9), and underscore (_) characters, not beginning with a digit, and not one
of the following Python keywords: and assert break class continue def del elif else
except exec finally for from global if import in is
lambda not or pass print raise return try while yield.+= or -= or *= or /= or %=| The block's statements are executed in order. |
[integer_expression]| The expressions are first evaluated, yielding a string and an index. It is an error if they are not of the indicated types. A new sequence (string or list) is returned that contains the value at the indicated index in the sequence. All Python indexing is zero-based. |
literal
| ||
| or |
| |
| or |
| |
| or |
| |
| or |
| |
| or |
| |
| or |
| |
| or | indexing | |
| or |
|
+ or - or not** or * or / or % or + or - or == or != or > or >= or < or <= or and or or or in or not in
| |
| or |
|
| or |
|
| or | None |
| or |
|
| or |
|
This table resolves ambiguities in the expression syntax. Operators with higher precedence are performed first. Operators on the same row have the same precedence, and higher precedence than those on following rows. Operators of equal precedence associate left to right, so for examplea * b / cis equivalent to(a * b) / c.slicing indexingattribute_referencefunction_call**(unary)+ (unary)-*/%+-innot in==!=>>=<<=
(unary)notandor
Strictly speaking, the following is not Python syntax, for built-in functions are accessed by a variable reference and module contents are accessed using a module attribute reference, as defined above.
Argument types are indicated in parenthesis. The typevalueindicates an argument that may be of multiple types, andnumberindicates either an integer or floating point value. Recall thatstris the string type andintis the integer type.
abs(number)bool(value)chr(int)file(str, str)float(value)help(value)int(value)len(str)list(str)max(value, value)min(value, value)ord(character)raw_input(str)round(float)str(value)sqrt(number)random()randrange(int)fix(float, int)get(url_string)find(str [, begin_index])index(str [, begin_index])isdigit()lower()rjust(int)strip()upper()append(value)index(value [, begin_index])sort()reverse()close()read()readline()write(str)