Python Rules Of Coding: Indentation

in Education5 years ago

Previous
Python Data Types

code-1084923_1280.png
Source

Python Rules Of Coding

Introduction :

For Python coding rules we follow Python's style guide known as PEP-8. Python Enhancement Proposal or PEP-8 has given the main focus on code to readability.
Although Python is relatively easy to learn than another programming language, it is important and a little bit hard to deal to know how to express better with the Python language.

Python language has a philosophy.
It can be seen when we type "import this" on Python IDLE.

py1.JPG

So 'Readability issues' should be our main concern while writing Python code. For this why, we should be obedient to PEP-8.
We can check whether our code complies with these rules using the Python style checker .

Python Rules Of Coding

Table Of Contents

  1. Indentation
  2. Naming Conventions
  3. comments
  4. String Quotes
  5. User Documentation

Indentation :

Indentation gets extra importance while coding python programming.
According to the Python style guide, the indentation should be 4 spaces per indentation level.

Look at below, here 'if' with 'print' statement is indented with 4 spaces -

py2.JPG

py3.JPG

Multi-line expressions don't follow 4-spaces. Multi-lines expression should be aligned vertically which is known as “hanging indent”.
The below examples from PEP 8 -- Style Guide for Python Code :

py4.JPG

py5.JPG

py6.JPG

Next

Python Rules Of Coding: Naming Conventions