Python Tutorials – Coding Layout

Spaces and Colons:

As we already seen in Python Introduction that Python is basically invented for simplicity and easy of understanding.

A well formatted code is easy to understand.

Even though we all know this, some may not consider this and writes without formatting. This leads to less readability.

Python is designed this as a major concern. It does not support curly braces {  }.

This is because, If { } are allowed then that means spaces and format doesn’t matter.

Even a if else ladder statement can be written in a single line.

This will increase the code complexity and decrease the code readability.

Instead, Python allows spaces and colons. Indentation will be used to differentiate the code blocks.

Each line will end with a new line (Enter).

Whenever a new block starts, the code inside block will be preceded with 4 spaces.

It can be any number of spaces but it should be same in all the lines in same block. Usually programmers will use 4 spaces.

For example:

In Java, an IF condition:

if(booleanVariable){ 
   System.out.println(“True condition”);
} else {
   System.out.println(“Else condition”);
}

In Python:

booleanVariable = True

if booleanVariable:
    print('True condition')
else:
    print('Else condition')

Default image
Asha Ponraj

Data science and Machine Learning enthusiast | Software Developer | Blog Writter

Articles: 84

Leave a Reply