Examining syntax rules
There are nine fundamental syntax rules in section 2.1 of the Python Language Reference. We'll summarize those rules here:
- There are two species of statements: simple and compound. Simple statements must be complete on a single logical line. A compound statement starts with a single logical line and must contain indented statements. The initial clause of a compound statement ends with a
:
character. It's possible, using rules 5 and 6, to join a number of physical lines together to create a single logical line.- Here's a typical simple statement, complete in a single logical line:
from decimal import Decimal
- Here's a typical compound statement with a nested simple statement, spread across two logical lines:
if a > b: print(a, "is larger")
- Here's a typical simple statement, complete in a single logical line:
- A physical line ends with
\n
. In Windows,\r\n
is also accepted. - A comment starts with
#
and continues to the end of the physical line. It will end the logical line.- Here's an example of a comment...