Introduction – What is a comment?
A comment is a line of code of a software program that does not affect the execution of a program. It is ignored by the compiler and interpreter programs and is there only to communicate something to the person reading the code.
Comments are commonly used in programming languages. Although a recommended way to increase the readability of one’s code, programmers take a while to develop the healthy habit of using comments to the best of their potential.
In this article, we will dive deep into the usage of comments in Python programming.
Purpose of comments in a code
Even though they do not play any direct role in program execution, comments are not useless either. Following are the purposes they serve in a program’s code:
- Enhanced readability for the code writer when they need to revisit the code after months
- Better team collaboration as everyone understands the code written by the other member
- Commenting out code for testing
Basics of Python comments
Every language has a different syntax for comments. Python uses a ‘#’ (hash mark) to convey to the interpreter to ignore the text after the hash symbol.
- Code:
#Interpreter ignores this.
#This is a comment
print(“This gets printed.”) #This is the python print function.
#print(“This does not get printed.”)
When you run this code, the only thing the output shows is This gets printed. Everything else gets commented out.
The example shown here demonstrates the usage of Python comments. However, comments should be used to explain what a particular line of code, block, or function achieves for the program.
Also, comments should always be indented at the same level as the code it seeks to explain.
Code:
#For Loop iterates from i=1 to i=9
for i in range(1,10):
print( i )
Inline Comments in Python
While comments find use in explaining the upcoming lines of code or function, inline comments are specific to only a statement. They are written right after the statement. They start with a ‘#’ just like any other comment and are generally shorter in the word count.
Inline comments should be used sparingly, as they have the potential to congest the code. Programmers use them when there is a complicated logic to a line in the code, which might not be obvious to other team members or even themselves after a few months.
For example:
X= 8 + pi * theta #This line creates a random number for us on each iteration.
Multiline Comments in Python
Technically, Python does not have a multiline comment facility.
- Code:
# I wanted to
create a multiline comment.
You can try something like this, only to realize that the interpreter ignores the first line and shows an error in the next one.
Still, there are a few workarounds that Python programmers use to create multiline comments, like the following.
- Block comments
Block comments are nothing but multiple single-line comments. At the beginning of each line, the programmer uses a hash mark to comment out that single line. When this is done multiple times over, the result is a longer multiline comment, also called a block comment.
- Code:
#I wanted to
#create a multiline
#comment.
While this looks like an unsophisticated solution, even professional Python programmers use it for their projects of all scales. Block comments offer a very convenient way to explain complicated code that needs a longer explanation than a few words.
- Triple quotes
Using a triple quote to add a multiline comment is a well-known practice among Python practitioners. Technically though, this is not a comment but a string. But, as the string is neither assigned anywhere nor referenced for any task, its presence in the code doesn’t impact the outcome.
- Code:
"This is a multi-line comment
that people often use in Python. However,
this is not really a comment but an unassigned
string. If we run this code, this string will not
appear in the output. "
Commenting out code while testing
In addition to increasing the code’s readability, Python programmers often use comments in a slightly different manner. Developers can comment to exclude the code they do not want to use at the moment. It helps them debug the code better and focus specifically on the part that interests them.
- Code:
predict = model.predict_generator(test_generator,steps = nb_samples)
evaluation=model.evaluate_generator(test_generator,steps=nb_samples)
#import matplotlib.pyplot as plt
#plt.plot(range(nb_samples),predict)
#plt.xlabel('index of test images')
#plt.ylabel('Prediction')
#plt.title('Predicting Classes')
#plt.show()
Here, the plotting part has been commented out to focus only on the predict and the evaluation variables.
Conclusion
Python comments provide a very helpful functionality that improves the productivity of a programmer in the long run. It not only makes the code more readable but also increases the efficiency of team collaborations on big projects. A good programmer writes good code; a great programmer adds comments to it.
For a Free trial - https://www.e2enetworks.com/cloud-credits-request-ks/