Line Indentation In Python - Python: Programming Language for Beginners - Learn In A Day! (2015)

Python: Programming Language for Beginners - Learn In A Day! (2015)

Chapter 4. Line Indentation In Python

One unique feature of Python is that, it does not allow braces to denote a block of code. In languages like C or Java, we use braces ({ }) to denote code blocks. But in Python, line indentation is used to denote blocks of code. The programmers need to be cautious about the line indentation as Python strictly enforces it. Adding spaces to the left of the code lines to construct a block structure is called indentation.

Consider the following example:

print 'The cat is fluffy'

print 'The dog is pudgy'

print 'The bird is chattering'

The above script generates no error and gives the output as:

The cat is fluffy

The dog is pudgy

The bird is chattering

Now, try changing the indentation of the second line as follows:

print 'The cat is fluffy'

print 'The dog is pudgy'

print 'The bird is chattering'

The above script generates an ‘Indentation Error’; it means all the three statements are supposed to form a single block and hence should have the same amount of indentation.

The user can use variable number of spaces as the indentation. But, care must be taken that the statements within a single block should have the same number of spaces as indentation.

If you are familiar with C language, take a look at the if-else statement written in C:

if(5<7)

{

printf ("The given condition is true");

printf ("abc");

}

else

{

printf ("This given condition is false");

printf ("xyz");

}

The above C code can be written in Python as follows:

if 5<10:

print 'The given condition is true'

print 'abc'

else:

print 'The given condition is false'

print 'xyz'

Notice the differences between the syntax of the two languages. And also, notice that indentation has been used in Python instead of braces ({}) to specify a block.

Consider changing the indentation of one of the print statements from the above code as follows:

if 5<10:

print 'The given condition is true'

print 'abc'

else:

print 'The given condition is false'

print 'xyz'

OUTPUT:

Line 3

print "abc"

^

Indentation Error: unindent does not match any outer indentation level

In the above code, in line 3, the print statement deviates from the indentation of all the print statements, and hence an error is generated.

Take a look at another example in Python:

Block 1:

if 5<10:

print "abc"

else:

print "xyz"

OUTPUT: abc

In the above block, if and else should have the same indentation, as they together form a control statement block. Similarly, the print statements should have the same indentation, as each print statement is considered a block of code.

However, the following block will give an error:

Block 2:

if 5<10:

print "abc"

else:

print "xyz"

OUTPUT:

Line 2

print "abc"

IndentationError: expected an indented block

An error is generated as the if else statement and print statements are all given the same indentation.

Consider another block of statements:

Block 3:

if 5<10:

print "abc"

else:

print "xyz"

The above code gives the output as follows:

OUTPUT:

Line 1

if 5<10:

^

IndentationError: unexpected indent

Hence, care should be taken while indenting the lines of Python code. As for spaces between the lines, any number of blank lines can be left between any two lines of codes.

Example:

if 5<10:

print '5 is less than 10'

print "abc"

else:

print "xyz"

In the above block of code, 5 lines have been left blank between the if statement and the else statement. Still, it does not have any effect on the output and no error is generated. The programmers need to be cautious only about the line indentation.

Note: Even though any number of spaces can be given as indentation, it is a good practice to use 4 spaces as indentation.

Multi-Line Statements:

In Python, a statement ends with a single line. A new line indicates that the statement in the previous line has ended. In other words, Python does not allow a single statement to be written in multiple lines.

Example: Consider a program which is used to find sum of three numbers

a=10

b=5

c=5

sum= a+b+c

print sum

OUTPUT: 20

Now, try rewriting the code by splitting up the expression in line 4 into two lines as follows:

a=10

b=5

c=5

sum= a+

b+c

print sum

OUTPUT:

Line 3

sum= a+

^

Syntax Error: invalid syntax

In the above program, line 4 has only a part of a statement, with its continuation in the following lines. Yet, the interpreter assumes that the statement has ended in the single line. Hence, an error is generated as the expression ‘sum = a+’ has no meaning or syntactic relevance

Consider writing the same program as follows:

a=10

b=5

c=5

sum= a

+b+c

print sum

OUTPUT: 10

The above code does not generate an error but does not give the desired output. It is because, since the statement in line 4 ends with ‘a’, the interpreter assumes that it is the end of the statement. Since the assignment operator ’=’ (you will learn about it in the next chapter) is used, the interpreter assigns a’s value to sum, making it 10.

However, we can write a single statement in multiple lines using the continuation character (\).

For example:

a=10

b=5

c=5

sum= a+\

b+c

print sum

OUTPUT: 20

The above code does not generate any error even though the expression ‘sum = a+b+c’ is written in two lines, as the continuation character (\) is specified at the end of the first line of statement (line 4 in the code).The continuation character (\) indicates that the statement has not ended with the end of the line, but continues to the next line. However, it is not necessary to use the continuation character for the statements written inside the (), [] or {}.

Example:

Thirty_day_months = ['April', 'June', 'September', 'November']

The above statement can also be written as

Thirty_day_months = ['April', 'June',

'September',

'November']

Thus it is possible to write multi line statements in Python.