This article is about indentation in Python. Why? Because Python is VERY stringent about indentation being correct. Forgetting to indent is also a common mistake that new programmers often make when writing a Python program.
When and How Indents are Needed
Any time a Python statement ends with a colon (":"), the next line must be indented. If one of the indented lines has a colon, the line following it must be indented further. Each group of lines (indented or not indented) must have the same indentation. Lines outside of these groups must NOT be indented. Common statements that must end in a colon include:
Indentation must be consistent. It must be the same throughout the program. So, if you decide to indent using 2 spaces, ALL indentations must be 2 spaces. If you use 4 spaces, all indentations must be 4 spaces. If you decide to use tabs for indentation, you must use tabs for all indentations through the program. You cannot mix different ways of indenting the code. It must be the same method in all cases.
Simple Python programs do not have any indentation. These are typical of what you will produce during the first week of this course. The image below illustrates a simple program:
Programs that include control statements or functions will always require colons and indentation. The image below illustrates a program with a single control statement and an indentation using a tab. The Python interpreter will throw an error if line 18 is not indented. However, it could be indented with 2 spaces or 4 spaces, rather than a tab.
Another IF-Statement Example
Here is an example of an if statement. It must end in a colon. The if statement starts a new block of statements below it. Any statements that should be a part of that block should be indented. Once a statement goes back to a previous level of indentation, the block ends. In this case, there are two statements that will be executed when the if statement is true. Neither will be executed when the if statement is false. The last statement is outside of the block and will always be executed.
Continuing Statement Example
This example illustrates the case where multiple control statements are really part of the same block, and are a continuation of each other. This is an example of an if-else statement. The if statement must end in a colon. Any statements that should be included when the statement is true, should be indented.
The else statement is part of the if statement; it is a continuation of that statement. As such, it should be indented at the same level (the same number of indentations) as the if statement. It may then have one or more statements that are indented and part of the "else" logic.
Nested Statement Groups Example
Examine the example below. Every time a statement ends in a colon, it starts a block of statements that must be indented together. If one of the statements in that group also ends in a colon, it starts a new group of statements that must be indented together. Each indent is referred to as a "level"
Remember also that all indentations must use the same method. The Python interpreter will catch cases where indentations do not use the same method.