polo
polo

Reputation: 1452

What's the syntax of Python Simple Statements?

I read this documentation: http://docs.python.org/reference/simple_stmts.html

Now, I want to create statements like it describes. For example, a statement that concat a multiple assert and print statements. The syntax is unclear. How would I use the ::= operator?

I will be grateful for a clear example.

Upvotes: 1

Views: 988

Answers (1)

Jon Purdy
Jon Purdy

Reputation: 55049

I think you are confusing the Python grammar reference with examples of actual Python code. The sections with ::= are formally describing the structure of Python statements in Backus–Naur Form. The other examples show actual Python code, and how the formal grammar looks in practice.

For example, the grammar element assert_stmt has the form:

assert_stmt ::= "assert" expression ["," expression]

This describes the structure of an actual Python assert statement, for example:

assert (2 + 2 == 4), "The world is ending!"

The quoted elements in the grammar, called terminals, appear literally in the Python syntax. These include, for example, the assert keyword and the comma. The unquoted parts refer to other grammar elements, called nonterminals; for example, expression corresponds to a Python expression returning a value. Grammar elements in brackets [] denote optionality, so expression ["," expression] refers to a comma-separated list of one or two expressions.

Upvotes: 14

Related Questions