Looking at pages of codes can sometimes be exhaustive for an average human. After all, code is just a language used to make machines understand our requirements. Being someone who often finds out of depth when the core language skills of our native tongue are tested, it is no wonder that we find it difficult to put up with coding which is comparatively new to mankind. So, we resort to our own language to assist our understanding of codes.
Yep! Comments play an integral role in conveying to the user the purpose of any particular code in a program. Let us learn about comments in Python in detail in this article.
What are Comments & Why do we need them?
Comments are textual descriptions within a program that describes the purpose or function of any particular code in a program. That being said, these textual data are skipped by the Python compiler when the program is run making it a stealthy accomplice.
Not all programs are crisp & short with a few lines of code unless it’s some examples for a demonstration to a school kid. Realtime applications involve programs that when printed can be the equivalent of volumes of books. Thusly, to understand the interlinks & functionality of the codes in different sections, it is recommended for the developer to leave a few words about what-is-what & which-is-which.
How to Identify a Comment?
All comments in Python start with a hash (#) making it all the way easier to be spotted by the developer or other users and at the same ignored by the compiler during execution of the program. There is also another way of commenting using the triple double quotes (” ” “) which shall be explained in the later sections of this article.
Implications of Deploying Comments
Listed below are some of the key uses of deploying comments in your Python program:
- Serves as a documentation of the program explaining its coding.
- Provides more information about the complicated logic behind the codes, thereby assisting in understanding them.
- Supports during times of code edits.
- Reduces time spent when spinning off an update to the existing program.
Creating Comments – Best Practices
The objective behind comments is to provide a glimpse of logic, not write paragraphs explaining the same. So, it is imperative that they are kept short and crisp. There are also no special locations where the comments are to be deployed in a program. One can feel free to put a comment wherever required, but not repeat them wherever the code repeats.
The other important part about writing comments is not to write them. You read that right! Assigning proper names to the identifiers does a lot of self-explanation relieving us from writing lines of comments. Too many comments might also make the program look a bit odd and clumsy.
Types of Comments in Python
There are two methods of placing comments in Python programming, each of which is explained below.
- Single-Line Comments – It starts with a hash (#) symbol followed by the textual description. In case multi-line comments are needed, the same can be done by using multiple hashes on each new line to write the comment. Let us look at a few examples of how comments are written using hash.
#This is how a comment is written
print("Hello Python")
- Multi-Line Comments (Docstrings) – This is a way to include multiple lines of comments in Python. Given below is an example in which triple-double quotes are used to include multiple lines of comments within the given set of codes.
"""
Technique to type
multi line comments
in Python. Let's multiply
2 numbers.
"""
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
z = a*b
print("Product of given numbers: ",z)
Conclusion
Now that we have reached the end of this article, hope it has elaborated on how to create comments for effective utilisation and the best practices associated with it in Python. Here’s another article that can be your definitive guide to the map( ) function in Python. There are numerous other enjoyable and equally informative articles in CodeforGeek that might be of great help for you to gain valuable insights. Until then, ciao!
Reference
https://docs.python.org/3/tutorial/introduction.html