Hings to Know about Control Structures

This section describes some general things you need to know about how control structures work. It applies to all the structures in this chapter.

• A control structure starts with a keyword—if, for, try, while, or with. The first line also contains an expression and ends with a colon.

• You can nest control structures. So an if statement can be nested inside a for loop, and vice versa. (You can also nest for in for and if in if.)

• Python uses indentation to group statements together.

Each line within a code block must be indented by the same amount—the convention is four spaces for the first block, eight spaces for any block nested in the first block, and so on.

• When you enter a compound statement (a statement with more than one line) in interactive mode (see Chapter 2 for instructions), you need to end it with a blank line (a Return). This tells the parser that you have typed the last line of the block.

Was this article helpful?

0 0

Post a comment