How do we provide comments in Python?



In Python, comments are short summaries that are added to the code to make it more readable. They are used by programmers to record their thought processes when producing code. It explains why a certain line of code was written in the first place. They're just meant to help coders or other developers comprehend a piece of code, especially since the Python interpreter ignores comments entirely.

In Python, comments can be used in a variety of ways.

The following are some examples of important applications −

  • Improved readability.

  • Educating others about the code.

  • After a long period of time, you will be able to readily understand the code.

  • Resources are included.

  • Repurposing existing code.

The following is shows the comment in Python which start with a hash mark (#) and ends with a whitespace character.

# This is a comment

Example

The following is an example which shows the representation of single line comment.

#python program to print “Hello, World!” to console print("Hello, World!")

Output

On executing the above program, the following output is generated.

Hello, World!

Block Comments

Block comments can be used to clarify more sophisticated code or code that the reader is unlikely to understand. These longer form comments are indented at the same level as the code and apply to some or all the code that follows.

Each line in a block comment starts with a hash mark and a single space. If you need to utilize more than one paragraph, a line with a single hash mark should be used to divide them.

Example

The following example is a python program to get the path of the current working directory. The top 4 lines are the explanation of the program, and they are commented with the help of # symbol.

#Python program to get the path of the current working directory #Program to get the path of the file #Using getcwd() #Importing the os module import os print(' The current working directory is: ', os.getcwd()) print('File name is: ', __file__)

Output

On executing the above program, the following output is generated.

The current working directory: C:\Users\pranathi\Desktop\python prog
File name: c:\users\pranathi\desktop\python prog\untitled1.py

Inline Comments

Following the code, inline comments appear on the same line as the statement. They start with a hash mark and a single whitespace character, much like regular comments.

Example

In the following example, the inline comments are shown. The comment that is appeared beside the print statement is the inline comment.

print("Hello, World!") #python program to print “Hello, World!” to console

Output

On executing the above program, the following output is generated.

Hello, World!

Python Docstrings

For commenting on modules, methods, functions, objects, and classes, Python has a built-in tool called docstrings. They are written using three quote marks (" or "") in the first line after declaring a module, function, method, or other object. The interpreter will not accept it as a docstring if it is not used in the first line. The __doc__ attribute can also be used to obtain docstrings.

Example

In the following example, the comments are represented with the three quote marks.

'''program to print hello world''' print("Hello, World!")

On executing the above program, the following output is generated.

Hello, World!

Advertisements