AlphaCodingSkills

Python - Comments


Advertisements

Previous Page Next Page

The Comments are added in programming code with the purpose of making the code easier to understand. It makes the code more readable and hence easier to update the code later. Comments are ignored by the compiler while running the code. In python, there are two ways of putting a comment.

  • Single line comment
  • Multi-line comment

Single line Comment

It starts with # and ends with the end of that line. Anything after # to the end of the line is a single line comment and will be ignored by the compiler.

Example:

In the below example, two line comments are used. Comments are ignored by the compiler while running the code.

# first line comment
print('Hello World!.') # second line comment

Output

Hello World!.

Multi-line Comment

Python does not have multi-line comment functionality however this can be achieved by starting each line by #. The same can be achieved by enclosing text between tripe quotes (""" ... """ or ''' ... '''). Compiler considers the text wrapped inside triple quotes as a comment as long as it is not assigned to any variable.

Example:

In below code, multi-line comments are used which is achieved by either starting each line by # or enclosing text between triple single quotes '''...'''.

#comment line 1
#comment line 2
'''
comment line 3
comment line 4
'''
print(  'Hello World!.') 

Output

Hello World!.

Example:

In below code, multi-line comments are used which is achieved by enclosing text between triple double quotes """...""".

"""
comment line 1
comment line 2
"""
print(  'Hello World!.')  

Output

Hello World!.

Previous Page Next Page