Sintaxes completas representadas por Diagrama Sintático - Diagrama de Sintaxe (ver convenções de Diagrama Sintático) ou BNF (ver convenções de Lazy BNF).
file_input: [(NEWLINE | stmt)+] ENDMARKER
single_input: stmt? NEWLINE
eval_input: test {',' test} ','? [NEWLINE+] ENDMARKER
stmt: small_stmt (';' small_stmt)* ';'? |if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | [decorator+] (classdef | funcdef)
if_stmt: 'if' [(test ':' suite 'elif')+] test ':' suite ['else' ':' suite]
>>> if x < 0:... x = 0... print 'Negative changed to zero'... elif x == 0:... print 'Zero'... elif x == 1:... print 'Single'... else:... print 'More'
while_stmt: 'while' test':' suite ['else' ':' suite]
>>> # Fibonacci series: ... # the sum of two elements defines the next... a, b = 0, 1>>> while b < 10:... print b... a, b = b, a+b
for_stmt: 'for' '*'? expr (',' '*'? expr)* ','? 'in' test (',' test)* ','? ':' suite['else' ':' suite]
>>> # Measure some strings:... a = ['cat', 'window', 'defenestrate']>>> for x in a:... print x, len(x)
with_stmt: 'with' test [ 'as' expr ] ':' suite
decorator: '@' NAME ('.' NAME)* [ '(' arglist')' ] NEWLINE
classdef: 'class' NAME ['(' arglist ')'] ':' suite
funcdef: 'def' NAME '(' typedargslist')'['->' test] ':' suite
small_stmt: expr_stmt | 'del' '*'? expr (',' '*'? expr)* ','? | 'pass'| flow_stmt | import_stmt | ('global '|' nonlocal') NAME (',' NAME)* |'assert' test [',' test]
expr_stmt: test (',' test)* ','? ('+ - * / ** & | ^ < < > > //' '=' (yield_expr | test (',' test)* ','?)|('=' (yield_expr | test (',' test)* ','?))+ | "")
flow_stmt: 'break'| 'continue' | 'return' [test (',' test)* ','?] | 'raise' [test ['from' test]] | yield_expr
import_stmt: 'import' dotted_as_names | import_from
import_from: 'from' ([('.' | '...')+] NAME ('.'NAME)* | ('.' |'...')+) 'import' ('*' | '(' import_as_names ')' | import_as_names)
import_as_names: NAME ['as' NAME] (',' NAME ['as' NAME])* ','?
try_stmt: 'try' ':' suite((except_clause ':' suite)+ ['else' ':' suite] ("" | 'finally' ':' suite) | 'finally' ':' suite)
except_clause: 'except' [test ['as' NAME]]
typedargslist: ([name_test ',']('*' tfpdef? [',' name_test] [',' '**' tfpdef] | '**' tfpdef) | name_test? ','?)
name_test: tfpdef ['=' test] (',' tfpdef ['=' test])*
tfpdef: NAME [':' test]
test: or_test ['if' or_test 'else' test] | 'lambda' varargslist ':' test
suite: small_stmt (';' small_stmt)* [';'] NEWLINE | NEWLINE INDENT stmt+ DEDENT
arglist: (argument ',')* (argument [','] | '*' test (',' argument)* [',' '**' test] | '**' test)
argument: test (comp_for? | test '=' test)
varargslist: name_test_list ',' ('*' [NAME] [',' name_test_list] [',' '**' NAME] | '**' NAME)| name_test_list [',']
name_test_list: NAME ['=' test]{',' NAME ['=' test]}
yield_expr: 'yield' [test (',' test)* ','?]
or_test: ('not'+)? '*'? expr("< | > | == | >= | <= | != | in | not in | is | is not" '*'?expr)* (('and' | 'or') ('not'+)? '*'? expr("< | > | == | >= | <= | != | in | not in | is | is not" '*' ?expr)*)*
expr: '+ | - | ~'? atom [trailer+]('+ - * / % ** & | ^ << >> //' '+ | - | ~'? atom [trailer+])*
atom: ('(' ([yield_expr]|testlist_comp) ')'|'['testlist_comp']'|'{'dictorsetmaker '}' |NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
testlist_comp: [test (comp_for | [(',' test)+] [','])]
dictorsetmaker: test ( (':' test (comp_for |[(',' test ':' test)+] ','?)) |[(comp_for | ',' test)+] ','? )
trailer: '(' arglist ')' | '[' subscript (','subscript)* ','? ']' |'.' NAME
subscript: test | [test] ':' test? (':' test?)?
dotted_as_names: NAME ('.' NAME)* ['as' NAME](',' NAME ('.' NAME)*['as' NAME])*
comp_for: ('for' '*'? expr (',' '*'? expr)* ','? 'in' or_test [('if'test_nocond)+])+
test_nocond: or_test | 'lambda' varargslist ':' test_nocond