Python Comments
As we know that python is an easier programming language than the other programming languages. But if we talk about large projects or team projects, then it difficult to understand the codes in an effective time for better results. Python comments can be used to both hide and explain and hide the code.
How Comments help us?
- Comments can be used to make the code more readable. This helps when new people join some project and read the project code first time.
- Description inside comments is not executed while testing the code. This helps in debugging of the large program.
Types of Python Comments
According to syntax and working, python comments can be categorized into two types:
- Single-Line Comments
- Multiple line Comments
Now we are going to learn about both types of comments briefly.
Single Line Comment
Single line comment allows us to comment on one line of code. Python single Line Comment starts with #
. And there is no need to end the single-line comments. Whatever, we write on the right side of # on that line, it will be treated as a comment. The comment text will not affect the output of the program.
Example of a Single Line Comment
Let’s see a simple example of single-line comments in the Python Program.
# This is the simple print statement
print("Welcome to the Tutorials Class")
Welcome to the tutorials Class
Note: It is also possible to add comments at the end of the code’s line such as:
print("Welcome to the tutorials Class") # This is the simple print statement
Python Multi-line Comments:
Multi-line Comments in Python allows us to mention comment in the multiple lines. There is a need to end the multi-line comment.
Creating multi-line comments
We can create multi-line comments in the following ways:
Method – 1: Using Double Quotes Three times:
You can add multi-line comments using triple quotes """ """
. Let’s see a simple example of creating multi-line comment using the above syntax.
""" This is a multiple line comment
written in more than one line
useful for large descriptions """
print("Tutorials Class: All programming tutorials available here")
Tutorials Class: All programming tutorials available here
Method – 2: Using Single Quotes Three times
Multi-line comments can create repeating single quotes three times ''' Comment Text '''
. Let’s see a simple example of creating multi-line comment using this syntax.
'''
This is a another multi-line comment
written in more than a line
add as many lines as you want
'''
print("Tutorials Class: All programming tutorials available here")
Tutorials Class: All programming tutorials available here
Method – 3: Using # symbol on each line
Another simple way to create multi-line comments is by adding the #
symbol at the starting of every line of the comment. Let’s see a simple example of creating multi-line comment using the above syntax.
# This is a multiple line comment
# written in more than a line
# learn programming easily here
print("Tutorials Class: All programming tutorials available here")
Tutorials Class: All programming tutorials available here
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |