Python Data Presistence – Indents

Python Data Presistence – Indents

The use of indents is one of the most unique features of Python syntax. As mentioned above, each statement starts at the first character position of the next available line on the online shell. In the case of the script, blank lines are ignored. In many situations, statements need to be grouped together to form a block of certain significance. Such circumstances are the definitions of function or class, a repetitive block of statements in the loop, and so on. Languages such as C/C++ or Java put series of statements in a pair of opening and closing curly brackets. Python uses the indentation technique to mark the blocks. This makes the code visually cleaner than clumsy curly brackets.

Whenever you need to start a block, use: symbol as the last character in the current line, after that press Enter, and then press Tab key once to leave fixed whitespace before writing the first statement in the new block. Subsequent statements in the block should follow the same indent space. If there is a block within the block you may need to press the Tab key for each level of block. Look at the following examples:

Indented Block in Function

Example

>>> def calculate_tax (sal) :
. . .            tax=sal*10/100
. . .           if tax>5000:
. . .                     tax=5000
. . .          netsal=sal-tax
. . .          return netsal
. . .
>>>

Indents in Class

Example 

>>> class Example:
. . .              def init_(self, x) :
. . .              self.x=x
. . .             if x>100:
. . .                      self.x=100
. . .
> > >

Indents in Loop

Example

>>> for i in range (4) :
. . .         for j in range(4) :
. . .                       print ( i , j )
. . .