Python Comments

In python, comments are self-explanatory notes to provide information about the code that we wrote in our applications.

 

Using comments, you can prevent the execution of some code lines while testing and make the code more readable. In python, you can include the comments anywhere in the program. It won’t affect the application's performance because the comments won’t be compiled and executed by the compiler.

 

In Python, the hash (#) symbol is used to indicate the start of the comment line. Following is the example of creating the comments in python.

 

#Python Variables
a = 10
b = 20
print("a is greater than b") #Print a value
print("a is greater than b") # Print b value

If you observe the above example, we added the comments at the starting and end of the lines based on our requirements.

Python Multiline Comments

In python, you don’t have any specific syntax to comment on multiple lines in the python. So, to create multiline comments, you need to add a hash (#) symbol for every line.

 

Following is the example of creating the multiline comments in python.

 

#Multiline comments
#Declaring the variables
#Print values using print()
a = 10
b = 20
print("a is greater than b")
print("a is greater than b")

If you observe the above example, we added a hash (#) symbol at the beginning of multiple lines to comment multiple lines in python.

 

In python, there is no specific way to create multiline comments. In python, if you create string literals without assigning to a variable those will be ignored by Python Interpreter. So, you can create multiline comments in python is using triple quotes without assigning it to any variable.

 

To create multiline comments with triple quotes, you can consider using quotation character i.e. either single quote (') or double quotes (").

 

'''
Multiline comments
using string literals
'''
a = 10
b = 20
print("a is greater than b")
print("a is greater than b")

If you observe the above example, we created a multiline string literal without assigning it to any variable. So, the Python Interpreter will read and ignore the statement and use it as a multiline comment.

 

In python, if you create a string with triple single quotes (''') or double quotes (""") we will call it a docstring or documentation string.

 

This is how we can create the comments in python to provide detailed information about the specific block of code or line of code based on our requirements.