Control Flow - Code Blocks

The next tutorials are all about flow control. The tutorials teach different programming instruments which stand in the very core of programming - loops and conditions. Loops and conditions are statements that define a condition. The condition dictates if a certain piece of code is going to be executed. This code is placed inside a block.

How python "knows" when a block starts and when a block ends?
The answer is by indentations and colons. The Syntax:
     Code Block

Watch the example:

#An if block #The if statement if 1 == 1: #Notice that the next line has four spaces print ("1 equals 1") if 1 == 2: #The code won't be executed print("python is wrong") print("This code is executed no matter what")

Notice the syntax - after the statement there is a colon. The block comes under the statement with four spaces (most of the code editors can convert the tab key to 4 spaces).


The use of spaces in a new line is called indentation. Indentation is considered to be the "best practice" - it makes the code much more readable. Python is very unique because the indentation is much more than a "best practic" - it is an inherent part of the language. Python uses the identations as a way to find where a code block starts and ends. The programmer must make use of indentations or the code won't execute as expected!