Although comments do not change the outcome of a program, they still play an important role in any programming and not just Python. Comments are the way to improve the readability of a code, by explaining what we have done in code in simple english.
We can have any number of single and multi-line comments in a Python program. When you are working on a Project with your team, it becomes essential to use comments so that the other programmer is able to understand what you have done in the source code by looking at the comments.
Python Comments Syntax
In python we use # character to start the comment. Lets take few examples to understand the usage.
Single line comment:
# This is just a comment. Anything written here is ignored by Python
To have a multi-line comment in Python, use # character in front of every line.
# This is # another # comment
Example of Python Comments
In this Python program we are seeing three types of comments. Single line comment, multi-line comment and the comment that is starting in the same line after the code.
# We are writing a simple program here # First print statement print("Hello Guys") # Second print statement print("How are You all?") print("Welcome to BeginnersBook") # Third print statement
# character inside quotes
When # character is encountered inside quotes, it is not considered as comment. For example:
print("#this is not a comment")
#this is not a comment