Comments and documentation help make any program comprehensible to everyone. Suppose we write a huge program or develop a software. If someone looks at that code, it’s natural for them not to understand it. Therefore, the best practice is to use enough comments in the code and, if possible, maintain documentation. Here, we will learn about commenting and documentation in Python.

Comment

Comments in code are generally used to note something down or to write what a particular function or variable is used for. In the previous part, we wrote a Hello World code, let’s repeat that here:

print("Hello World")

Since this is a very short code, it’s fine not to use comments here. But what about larger code? Let’s write this code using a comment:

# This code is to display "Hello World" in console
print("Hello World")

The output of the above code will be the same as before. But here, a new programmer can easily understand what this program does.

In Python, a comment is made by placing a # symbol at the beginning of the line. That means any statement before # will be considered as a comment and it will not be displayed as part of the main program’s output.

What if we need to comment several lines? We can put a # symbol at the beginning of each line. However, Python has an even simpler solution for this. Let’s look at the following code:

"""
Created on Fri Jun 21 18:13:18 2019
@author: SHAHINUR
"""

# This code is to display "Hello World" in console
print("Hello World")

The output will still be “Hello World”; but here, we’ve included my name and the date, meaning we have commented multiple lines. For multiple line comments, we start and end with triple quotes “””.

In summary: Use # for single-line comments and “”” for multiple-line comments.

0 0 votes
Article Rating
0
Would love your thoughts, please comment.x
()
x