A Complete Guide To Writing Comments In Python
And why you should use them.
Many beginner programmers often focus on the function and efficiency of their code but fail to realize that they are missing out on the important concept of comments. Being what separates amateurs from proficient programmers, adding comments to our programs is essential, especially in longer and more complex ones. Comments enable other programmers to understand the idea and function of a certain block of code. They are also important when writing large programs, as they can remind us of the purpose of a certain block of code.
Below, you will find a detailed summary of the main types of comments in python and how they are used.
1. Inline Comments
In Python, inline comments are comments that are placed either before, after or on the same line of a line of code. As mentioned previously, they are written to explain the purpose and function of that line of code but should remain concise and to the point. Inline comments are written using the hashtag ‘#
’ and should generally not exceed 72 characters. There is no particular syntax to follow when writing them. However, proper formatting should be followed, as shown below.
Inline Comments Example:
print("Hello!") # Prints out greeting