Blocks of code

Blocks of code are groups of statements you can use where single statements are expected.

Blocks of code can follow any of the following statements: if, elif, else, for, while, try, except, def, and class. These statements introduce the block of code with the colon character (:). For example:

if x == 1: 
    y = 2
    z = 3
elif:
    y = 4
    z = 5

Use indentation to delimit code blocks (rather than the curly braces used in Java). All lines in a block must be indented to the same position. This is because a change in the indentation indicates the end of a code block. It's common to indent by four spaces per level. We recommend you use spaces to indent the lines, rather than tabs. Spaces and tabs must not be mixed. The lines in the outermost block of a module must start at column one, or a SyntaxError will occur.

The statements that make up a code block (and follow the colon) can also be on a single line, separated by semicolons. For example:

if x == 1: y = 2; z = 3;