As a programmer, it’s important to make sure that your code can be easily understood by others.
Comments are an integral part of any program. They are texts added to the program to provide explanatory information about the source code.
In this tutorial, we will cover some of the basics of writing comments in Python. You’ll learn how to write comments that are clean and concise, and why you need to write comments at all time.
WHAT ARE COMMENTS?
Comments are texts used in a programming language to document the program and remind programmers of what tricky things they just did with the code and also helps the later generation for understanding and maintenance of code. The compiler considers these as non-executable statements. Since comments do not execute, when you run a program you will not see any indication of the comment in the output. Comments are for developers. They describe parts of the code where necessary to facilitate the understanding of programmers, including yourself.
TYPES OF COMMENTS
- Single-Line Comments - Comments starting with a ‘#’ and whitespace are called single-line comments in Python. These comments can only stretch to a single line and are the only way for comments in Python. e.g.
# This a single line comment.
- Multi-line (Block) comments - Unlike other programming languages, Python doesn’t support multi-line comment blocks out of the box. However we can use consecutive # single-line comments to comment out multiple lines of code. Some examples of block comments.
# This type of comments can serve
# both as a single-line as well
# as multi-line comment in Python.
- Inline Style comments - Inline comments occur on the same line of a statement, following the code itself. Generally, inline comments look like this:
y = 6 # This is called an inline comment
x = a + b # Adding value of 'a' and 'b' to 'x'
Developers forget what their own code does all the time, especially if it was written a long time ago or under a lot of pressure. When a deadline is fast approaching, and hours in front of the computer have led to bloodshot eyes and cramped hands, that pressure can be reflected in the form of code that is messier than usual. Once the project is submitted, many developers are simply too tired to go back and comment their code. When it’s time to revisit it later down the line, they can spend hours trying to parse through what they wrote.
Writing comments as you go is a great way to prevent the above scenario from happening. Be nice to the future You!
Happy Coding!!!
RESOURCES
geeksforgeeks.org/how-to-write-comments-in-..
digitalocean.com/community/tutorials/how-to..
#2Articles1Week