Indentation in Python

Indentation has no effect on program logic—it is simply used to align program lines to aid readability. In Python, however, indentation is used to associate and group statements.

  • A header in Python is a specific keyword followed by a colon.
  • The set of statements following a header in Python is called a suite (commonly called a block).
  • The statements of a given suite must all be indented the same amount.
  • A header and its associated suite are together referred to as a clause.
  • A compound statement in Python may consist of one or more clauses. While four spaces are commonly used for each level of indentation, any number of spaces may be used.
  • In (a), both suites maintain the same level of indentation.
  • In (b), each suite has varying levels of indentation. This is syntactically correct, although it's not recommended practice. The key point is that the indentation within each suite remains consistent.
  • In (c), the if and else headers of the if statement aren't indented at the same level.
  • In (d), the headers have the same indentation, but the statements within the second suite lack proper alignment.