Thursday, August 2, 2018

Python Indentations and Comments


Indentations are not compulsory in other programming languages but in Python indentations are very important. In other programming languages like C, C++ and Java, we prefer indentations as it make the program codes clear to the programmer but it has nothing to do with the program.

Python programming language uses indentation to indicate a block of code.

Example
if a > b:
  statement1
  statement2
  statement3
statement4
Indentations make it clear to Python that statement 1,2 and 3 are within the if block and statement4 is not within the 'if body'.

Python will give an error if we skip the indentation:

Example
if a > b:
statement

In C,C++ and java the 'if body' would be defined as

if (a > b)
{
  statement1
  statement2
  statement3
}
statement4

OR (Without indentation)

if (a > b)
{
statement1
statement2
statement3
}
statement4




Comments in Python

Comments is necessary in all programming languages and Python also uses it's own comment style.

For single line comment, the symbol '#' before the start of the line.

Example


#This is not executable code
print("Hello, World!")

Docstrings in Python

Python uses doc strings (""".........""") to make multiple line comments.

Example


"""This is a
multiline docstring."""
print("Hello, World!")

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner