Learning Resources
 

Indentation and logical and physical lines


Logical And Physical Lines

A physical line is what you see when you write the program. A logical line is what Python sees as a single statement. Python implicitly assumes that each physical line corresponds to a logical line.

An example of a logical line is a statement like print('Hello World') - if this was on a line by itself (as you see it in an editor), then this also corresponds to a physical line.

Implicitly, Python encourages the use of a single statement per line which makes code more readable.

If you want to specify more than one logical line on a single physical line, then you have to explicitly specify this using a semicolon (;) which indicates the end of a logical line/statement. For example,

   i = 5
   print(i)

is effectively same as

   i = 5;
   print(i);

and the same can be written as

   i = 5; print(i);

or even

   i = 5; print(i)

However, I strongly recommend that you stick to writing a maximum of a single logical line on each single physical line. The idea is to avoid the semicolon as much as possible so that the code will be more readable. In fact, I have never used or even seen a semicolon in a Python program.

It is possible to use more than one physical line for a single logical line, but this should only be used if the logical line is really long. An example of writing a logical line spanning many physical lines follows. This is referred to as explicit line joining.

   s = 'This is a string. \
   This continues the string.'
   print(s)

This gives the output:

   This is a string. This continues the string.

Similarly,

   print\
   (i)

is the same as

   print(i)

Sometimes, there is an implicit assumption where you don't need to use a backslash. This is the case where the logical line uses parentheses, square brackets or curly braces. This is called implicit line joining

Indentation

Whitespace is important in Python. Actually, whitespace at the beginning of the line is important. This is called indentation. Leading whitespace (spaces and tabs) at the beginning of the logical line is used to determine the indentation level of the logical line, which in turn is used to determine the grouping of statements.

This means that statements which go together must have the same indentation. Each such set of statements is called a block. We will see examples of how blocks are important in later chapters.

One thing you should remember is that wrong indentation can give rise to errors. For example:

i = 5
 print('Value is ', i) # Error! Notice a single space at the start of the line
print('I repeat, the value is ', i)

When you run this, you get the following error:

     File "whitespace.py", line 4
       print('Value is ', i) # Error! Notice a single space at the start of the line
       ^
   IndentationError: unexpected indent

Notice that there is a single space at the beginning of the second line. The error indicated by Python tells us that the syntax of the program is invalid i.e. the program was not properly written. What this means to you is that you cannot arbitrarily start new blocks of statements (except for the default main block which you have been using all along, of course). 

How to indent - Do not use a mixture of tabs and spaces for the indentation as it does not work across different platforms properly. I strongly recommend that you use a single tab or four spaces for each indentation level.Choose either of these two indentation styles. More importantly, choose one and use it consistently; and be consistent with the style used in existing files you are editing. i.e. When writing new files use that indentation style only and if a file you need to edit is using tabs, while editing that file use tabs for indentation.
Note to static language programmers -Python will always use indentation for blocks and will never use braces. Run from __future__ import braces to learn more.
 
-Swaroopch