Structuring with Indentation

Most programming languages use certain characters or keywords to group statements:Blocks in Python through indentation

  • begin … end
  • do … done
  • { … }
  • if … fi

Python uses a different principle. Programs get structured through indentation, this means that code blocks are defined by their indentation. Okay that’s what we expect from any program code, isn’t it? Yes, but in the case of Python it’s a language requirement not a matter of style. This principle makes it easier to read and understand other people’s Python code.

So, how does it work? All statements with the same distance to the right belong to the same block of code, i.e. the statements within a block line up vertically. The block ends at a line less indented or the end of the file. If a block has to be more deeply nested, it is simply indented further to the right.

A statement can be continued on the next line with the continuation character “\”.

Leave a Reply