A201Python Syntax: Week 12

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.

Notation
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.
Comments and whitespace
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 they are part of a multi-line string literal, or follow a block and continue a compound statement.
Semantics
The 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.

Syntactic form definitions

program
The statements are executed in order.
statement
expression
The expression, which should be a function call, is evaluated. The returned value is discarded.
or
simple_variable_assignment
variable_name = expression
The value of the expression is assigned to the variable. If within a function definition, the variable is local to the function, and otherwise it is global. If a variable with the given name variable does not yet exist, one is created. Definitions and assignments that are not within a function definition and constitute the "top level" scope. Otherwise the scope is the enclosing function definition.
or
augmented_variable_assignment
variable_name augmented_assignment_operator expression
Equivalent to variable = variable operator expression, where operator is the first character of augmented_assignment_operator.
or
indexed_assignment
indexing = expression
The value of the expression is assigned to the list element indicated by the indexing.
or
delete_statement
del indexing
Delete the list element indicated by the indexing.
or
print_statement
print expression,...,expression
The expressions are evaluated and their values, separated by spaces, are output as a line of characters.
or
function_definition
def variable_name ( parameter_name,...,parameter_name ): body_block
A function with the indicated parameter names is create and assigned to the named variable. The parameter names are local to the body. The block is not executed at this time.
or
return_statement
return [expression] (only allowed in a function definition)
Immediately return the value of expression as the value of the call to the current function, or None if there is no expression.
or
import_statement
import module_name
The named module is loading and bound to variable module_name.
or
if_statement
if test_expression : then_block [elif test_expression : then_block]... [else: else_block]
Evaluate each test_expression in order until one has a true value, and then execute the corresponding then_block If none of the test expressions are true, execute the block.
or
break_statement
break
Exit immediately from the nearest enclosing while or for loop.
 (only allowed in a while or for statement block)
or
while_loop
while test_expression : body_block
Repeatedly evaluate test_expression until it is false, executing body_block after each time it is true.
or
for_range_loop
for variable_name in range( expression ): block
The expression's value should be an integer, n. The block is executed n times, with the variable assigned in turn to the integers 0,1,...,n-1. The block is not executed at all if n is less than 1.
name
augmented_assignment_operator
+= or -= or *= or /= or %=
block
The block's statements are executed in order.
indexing
sequence_expression[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.
expression
literal
The value indicated by the literal is returned.
or
variable_reference
variable_name
If there is a local variable with the given name, its value is returned. Otherwise the value of a global with the name is returned. It is an error if there is no variable the name.
or
unary_operation
unary_operator expression
The operator is applied to the value of the expression.
or
binary_operation
expression binary_operator expression
The operator is applied to the values of the expressions. If the operator is and or or, the second expression is not evaluated if the result is determined by the left experssion.
or
parenthetic_expression
( expression )
The value of the expression is returned.
or
function_call
function_expression ( argument_expression,...,argument_expression )
The value of the first expression, which must be a function or method, is applied to the values of the argument expressions. A variable is created associated with each of the function's parameters and bound to the corresponding argument value.

The the function block is then executed with these added variable bindings, after which the control returns to the point of call. If execution of the function block terminates with a return statement, the return statement expression's value is returned. Otherwise the value None is returned.

or
attribute_reference
object_or_module_expression . attribute_name
The object_or_module_expression is evaluated and the value should be an object or module. The named attribute, typically an object method or module function, is returned.
or indexing
or
slicing
sequence_expression [ [integer_start_expression] : [integer_end_expression] ]
The expressions are first evaluated, yielding a sequence (a string or list), a start index, and a stop index. It is an error if they are not of the indicated types. A new sequence of the same type is returned that is made of the sequence of values from the sequence beginning with the value indicated by the start index and ending with the value whose index is one less than the stop index. The start index defaults to 0 and the stop index to the lenght of the string.
unary_operator
+ or - or not
binary_operator
** or * or / or % or + or - or == or != or > or >= or < or <= or and or or or in or not in
literal
integer
A sequence of one or more digits not beginning with zero
or
float
A sequence of one or more digits containing one decimal point.
or
string
A sequence of characters beginning with a single or double quote character and ending, on the same line, with the next (unescaped) occurrence of the same quote character, or a sequence of characters beginning with three single or three double quote characters and ending (not necessarily on the same line) with the next (unescaped) occurrence of the same three quote characters.
Escape sequences include:
\` (single quote character)
\" (double quote character)
\n (newline character)
\t (tab character)
\\ (one backslash character)
or None
or
boolean
True or False
or

Operator precedence table

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 example a * b / c is equivalent to (a * b) / c.

slicing indexing
attribute_reference
function_call
** (unary)+ (unary)-
* / %
+ -
in not in == != > >= < <=
(unary)not
and
or

Built-in functions and modules

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 type value indicates an argument that may be of multiple types, and number indicates either an integer or floating point value. Recall that str is the string type and int is the integer type.
built-in functions
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)
Reeborg functions
math module
sqrt(number)
random module
random()
randrange(int)
fpformat module
fix(float, int)
http module
get(url_string)
string methods
find(str [, begin_index])
index(str [, begin_index])
isdigit()
lower()
rjust(int)
strip()
upper()
list methods
append(value)
index(value [, begin_index])
sort()
reverse()
file object methods
close()
read()
readline()
write(str)