Demystifying Comments in Python: Enhancing Code Readability and Documentation

 

Comments in PYTHON

Comments in Python are used to improve the readability of the code. Comments are non executable statements or ignore statements. Using these comments we can declare user defined or customized statements in the source code. Python supports two types of comments.

1 Single lined comment.
2 Multi lined Comment.

1) Single lined comment. (In Line Comments)

In Python, we use the pound (#) symbol to start writing a comment. If developer want to only specify one line comment than use single line comment, then comment must start with #

>>> #This is single line comment. (pound)
>>> #print("Hello")
>>> #print("Welcome to PYTHON Programming")

# This is a single-line comment
print("Hello, World!") # This comment is at the end of the line

Example :
As per Real Time Project(s) Use Case:
>>> #NameOfTheProject: BFSI
>>> #NameOfTheTask:GenerateWeeklyReports
>>> #TaskAssignedBy:TeamLead
>>> #TaskReviewedBy:TeamManager/Team Lead
>>> #ApproxTaskFinishedDate:01/11/2020
>>> #CommentsOnTask:
>>> #FeedBackFromClient

2) Multi-line comments

If we have comments that extend multiple lines, one way of doing it is to use pound (#) in the beginning of each line.

>>> #This is a long comment and it extends to multiple lines, This is a long comment and it extends to multiple lines.
''' This is a multi-line comment. It spans multiple lines using triple-quotes. ''' print("Hello, World!")

""" Another way to create a multi-line comment. Triple double-quotes also work. """ print("Hello again!")

Note:

It's important to note that while the triple-quoted string approach is commonly used for multi-line comments, these strings are technically creating string literals. However, because they are not assigned to a variable or used in any way, they serve the purpose of comments effectively.

Summary

Remember to use comments judiciously to explain complex sections of code, document your intentions, or provide information that might be useful to others reading your code. Keep in mind that code should be self-explanatory whenever possible, and comments should supplement, not replace, clear and readable code.

😊😊😊