26.5. Basic Python Syntax

Python follows a syntax that can be summed up with a few simple rules:

  • Comments are preceded by hash marks (#) and can begin anywhere in a line, although no active code may follow it on the same line. Multiline comments should also be indented, with each line preceded by a hash mark.

  • Blocks of code, called suites, are delimited with indentation, typically four spaces. Spaces and tabs should not be intermixed. Each time the level of indentation is increased, a new code block begins. The end of that code block is marked by the reduction of indentation to match the previous level.

  • A colon (:) separates the header of a code block from the rest of the suite.

  • Newline (\n) is the standard line separator.

  • Python statements are delimited by newlines, but single statements can be broken into multiple lines by using backslashes (\) to continue a line.

  • Functions are organized as importable modules. Each module is a separate Python file.

Get Web Standards Programmer's Reference: HTML, CSS, JavaScript®, Perl, Python®, and PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.