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 (#).
#printing 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
#this comment is an
#example of multi-line comment
- By enclosing the comment inside a set of triple quotes
This is another example of
to 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.