Table 10-1 summarizes Python’s statement set. Each statement in Python has its own specific purpose and its own specific syntax—the rules that define its structure—though, as we’ll see, many share common syntax patterns, and some statements’ roles overlap. Table 10-1 also gives examples of each statement, when coded according to its syntax rules. In your programs, these units of code can perform actions, repeat tasks, make choices, build larger program structures, and so on.
This part of the book deals with entries in the table from the top through break and continue. You’ve informally been introduced to a few of the statements in Table 10-1 already; this part of the book will fill in details that were skipped earlier, introduce the rest of Python’s procedural statement set, and cover the overall syntax model. Statements lower in Table 10-1 that have to do with larger program units—functions, classes, modules, and exceptions—lead to larger programming ideas, so they will each have a section of their own. More focused statements (like del, which deletes various components) are covered elsewhere in the book, or in Python’s standard manuals.
Table 10-1. Python statements
Technically, Table 10-1 reflects Python 3.X’s statements. Though sufficient as a quick preview and reference, it’s not quite complete as is. Here are a few fine points about its content:
Assignment statements come in a variety of syntax flavors, described in Chapter 11: basic, sequence, augmented, and more.
print is technically neither a reserved word nor a statement in 3.X, but a built-in function call; because it will nearly always be run as an expression statement, though (and often on a line by itself), it’s generally thought of as a statement type. We’ll study print operations in Chapter 11.
yield is also an expression instead of a statement as of 2.5; like print, it’s typically used as an expression statement and so is included in this table, but scripts occasionally assign or otherwise use its result, as we’ll see in Chapter 20. As an expression, yield is also a reserved word, unlike print.
Most of this table applies to Python 2.X, too, except where it doesn’t—if you are using Python 2.X, here are a few notes for your Python, too:
In 2.X, nonlocal is not available; as we’ll see in Chapter 17, there are alternative ways to achieve this statement’s writeable state-retention effect.
In 2.X, print is a statement instead of a built-in function call, with specific syntax covered in Chapter 11.
In 2.X, the 3.X exec code execution built-in function is a statement, with specific syntax; since it supports enclosing parentheses, though, you can generally use its 3.X call form in 2.X code.
In 2.5, the try/except and try/finally statements were merged: the two were formerly separate statements, but we can now say both except and finally in the same try statement.
In 2.5, with/as is an optional extension, and it is not available unless you explicitly turn it on by running the statement from __future__ import with_statement (see Chapter 34).