Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Examining syntax rules


There are nine fundamental syntax rules in section 2.1 of the Python Language Reference. We'll summarize those rules here:

  1. 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")
  2. A physical line ends with \n. In Windows, \r\n is also accepted.

  3. 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:

      from fractions import Fraction # We...