Javaexercise.com

What are comments in Python and How to create them?

Python is a high-level, interpreted programming language and one of the most popular programming languages present in the software industry.

One of the most important but often overlooked concepts in python is that of comments. Let us have a look at it in detail.

What are the comments in Python?

In python, comments are lines that are generally used to explain the code. These lines are ignored by the compiler.

The main purpose of comments is to explain to other programmers the intent of specific sections of code and is a part of healthy coding habits to enhance the readability of the code. 

Another use case of comments would be to prevent the execution of certain parts of the code. This practice is widely used by programmers for the purpose of debugging and identifying particular causes of errors. 

To better understand this concept, we shall follow a simple example of a piece of code that prints Hello World. Let us look at the python code and the corresponding output for this.

Python 3 Code

print("Hello World")

Output

Hello World

 

Now, let us look at different ways of adding comments to this code.

Single line comments in Python

Here we use a hashtag # to denote the start of a line as a comment as shown below.

This is followed by the required text we want to add as a comment. Anything you add after the hashtag is counted as a comment for that line.

Python’s single-line comments are useful for supplying short explanations for variables, function declarations, and expressions.

In some IDE or editors, you can also use the shortcut Ctrl + / to comment out a particular line. Let us look at the python code and corresponding output for this method.

Remember that comments do not have any effect on the output.

# This is a single line comment

print("Hello World")

Output

Hello World

 

Instead of using a hashtag at the start of the line, we use a hashtag # to denote the start of the comment as shown below, right after the piece of code to print.

This is followed by the required text we want to add as a comment. Anything you add after the hashtag is counted as a comment for that line. Let us look at the python code and corresponding output for this method. 

print("Hello World") # This is a single line comment 

Output

Hello World

Multiline comments in Python

Here, we use a hashtag # to denote the start of a line as a comment as shown below. This is followed by the required text we want to add as a comment. Anything you add after the hashtag is counted as a comment for that line.

Instead of writing the entire comment, we may need to split the comment into multiple lines, each line starting with a hashtag. To use the previously specified shortcut, Ctrl + /, you will have to use it on each line separately. Let us look at the python code and corresponding output for this method.

# This is a 
# multi line comment

print("Hello World")

Output

Hello World

 

An alternate method for multi-line comments would be to use three single quotes ‘ ‘ ‘ instead of using a hashtag separately for each line.

We use three quotes to denote the start of the multi line comment and three quotes to denote its end.

Let us look at the python code and corresponding output for this method.

'''
This is a 
multi line comment
'''

print("Hello World")

Output

Hello World

 

An alternate method for multi-line comments would be to use three double quotes “ “ “ instead of using a hashtag separately for each line.

We use three double quotes to denote the start of the multi line comment and three double quotes to denote its end.

Let us look at the python code and corresponding output for this method.

"""
This is a 
multi line comment
"""

print("Hello World")

Output

Hello World

 

Important use case of comments in Python

An important use case of comments would be to make changes in the code without removing the previous code completely.

For example, if we want to print Bye World instead of Hello World and keep the previous lines in the code for future reference, we could use comments.

Let us look at the python code and corresponding output for this method.

# print("Hello World")

print("Bye World")

Output

Bye World

Python Docstrings

Docstrings are a special type of comments that tell us about the uses and workings of the code. We can access the docstring of a function using the __doc__ property.

Let’s consider a simple function named hello that prints the message Hello World, add the corresponding docstring to it and then print the docstring.

Just like multi line comments, we can either use three single quotes or three double quotes

Let us look at the python code and corresponding output for this method using three double quotes.

def hello():

  """
  Function that prints Hello World
  """
  print("Hello World")

hello() # calling function
print(hello.__doc__) # calling docstring of the function

Output 

Hello World

Function that prints Hello World

 

Conclusion

In this topic, we have learned the use and advantages of comments in a Python program, following a running example of a simple Hello World program, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.