Comments are descriptions that are usually written while coding to enhance the readability and to better understand the functionality of the program. They are ignored by the interpreter while running the code.
Significance of commenting:
- In an organization, while working on a team, it is customary for many programmers to work on the same project. So it is always a good practice to write well commented function or logic to help the team understand.
- When reading or editing your own code, after a couple of months, it can be difficult to comprehend the program and can consume hours to understand what’s what! Writing comments while coding is an essential way to prevent this confusion from happening.
Ways to write comments in Python:
1. Single line comments:
Single line comments in Python are written using the hash symbol (#).
For example:
#printing hello world
print(“Hello World”)
2. Multi line comments:
Multi-line comments in Python can be written using the following two ways:
- By using multiple hash symbols at the beginning of each line of comment
For example:
#this comment is an
#example of multi-line comment
- By enclosing the comment inside a set of triple quotes
For example:
“””
This is another example of
multi-line comment
to print hello world!
“””
print(‘Hello World’)
Best Commenting Practices:
1. Comment in the beginning of the source file to describe the purpose of writing the code.
2. While making changes to any function or any other parts of the code written previously.
3. Make the comments as concise as possible.
4. Avoid writing rude comments as it could offend a team member.
5. Remove redundant comments.