• Conditionals
  • Loops
  • Function calls

Before going into these topics, we need to introduce the concept of a code block and understand its relationship to indentation, as they are very important in Python programming.

Code blocks and indentation

A code block is one or more lines of code that are intended to be executed as a set. For example, a code block may be a function body, or the statements within a loop.

In the C programming language, a code block must be enclosed by a pair of curly brackets. Other languages use additional keywords (e.g., done, enddo, fi) to denote the end of code blocks. But in Python, a code block is defined by the amount of indented space. Lines of code that have an equal amount of indentation, or blank spaces at the start of the line, are in the same code block. In the following example, lines 2 through 4 belong to one code block.

line1
    line2
    line3
    line4
line5

Indentation is mandatory in Python and must be exact. This is different from other programming languages like C, in which indentation is used only to improve the readability of the code. Adding spaces to improve readability in Python may produce syntax errors or, even worse, run time errors that can be very difficult to discover. It is important to note that tabs and spaces are interpreted differently in Python. Either tabs or spaces can be used but they should not be mixed (they are not the same thing, and the equivalent number of spaces a tab comprises depends on editor settings, rather than definition). According to the Style Guide for Python Code, spaces are preferred to tabs. Most text editors can be set to insert spaces when the tab key is pressed, and many text editors are by default or can be configured to be Python-aware so that sensible indentation patterns are supported when writing Python code.

Nesting

Code blocks can be nested. The following example demonstrates what a nested code block might look like:

condition1
    statement1
condition2
    statement2
    condition3
        statement3

Here, if condition1 is true, then statement1 is executed otherwise it is skipped. If condition2 is true, then statement2 is executed and condition3 is evaluated. Reason: statement2 and condition3 have the same amount of indentation and therefore belong to the same code block. However, if condition2 turns out to be false, then condition3 is never evaluated and statement3 cannot be reached.

In general, the following rules apply when programming in Python:

  • Statements within the same block of code need to be indented at the same level.
  • The amount of indentation matters: a missing or extra space in a Python block could cause an error or unexpected behavior.
Scope

The scope of a variable means all the parts of the program where the variable can be accessed. In Python, scope is controlled by indentation, too. Variables in the main body are global; they are accessible everywhere in the code, although if you want to be able to modify the value of such a variable, you must declare it as global within a subblock (e.g., a function body) using the global keyword. Variables defined in an indented block, though, are accessible only in that block and in its nested blocks. For example, variables defined in an if-block aren’t available outside that if-block. We’ll see this with functions, as well: not all variables are accessible from all parts of the program.

 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement