Comments in Python

In this module of Python tutorial, we will learn about Python comments. We will also learn about the different types of Python comments and the way to use them.

Writing Comments in Python


Comments in any programming language are used to increase the readability of the code. Similarly, in Python, when the program starts getting complicated, one of the best ways to maintain the readability of the code is to use Python comments. It is considered a good practice to include documentations and notes in the python syntax since it makes the code way more readable and understandable to other programmers as well, which comes in handy when multiple programmers are simultaneously working on the same project.
The code can only explain how it does something and not why it does that, but Python comments can do that. With Python comments, we can make documentations for various explanations in our code itself.
Interested in learning Python? Enroll in our Python Course in Bangalore now!

In this module, we will delve deeper into the concept of comments in Python. Following is the list of topics that are covered in this module.

So, without any further delay, let’s get started.

How to Comment in Python

Lets see how to comment in Python now. Comments are nothing but tagged lines of codes which increase the readability of a code and make it self-explanatory. There are different ways of creating comments depending on the type of comment we want to include in our code. Following are different kinds of comments that can be included in our Python program:

  • Single Line Comments
  • Docstring Comments
  • Multiline Comments

Let’s discuss each one of the above-mentioned comment types, separately.

Certification in Full Stack Web Development

Single Line Python Comment

Single line Python comments are marked with # character. These comments end at the end of the physical line, which means that all characters starting after the # character (and lasts till the end of the line) are part of the comment.
Example:

test= 7 * 2
print (test)
#Single-line comment
Output:
14

Get 100% Hike!

Master Most in Demand Skills Now !

Docstrings

Python has the documentation strings (or docstrings) feature which is usually the first statement included in functions and modules. Rather than being ignored by the Python Interpreter like regular comments, docstrings can actually be accessed at the run time using the dot operator.
It gives programmers an easy way of adding quick notes with every Python module, function, class, and method. To use this feature, we use triple quotes in the beginning of the documentation string or comment and the closing triple quotes at the end of the documentation comment. Docstrings can be one-liners as well as multi-liners.
Example:

def SayFunction():
”’
Strings written using ”’_”’ after a function represents docstring of func
Python docstrings are not comments
”’
print(“just a docstring”)
print(“Let us see how to print the docstring value”)
print(theFunction.__doc__)

Become a Full Stack Web Developer

Enrol yourself in Online Python Training in Sydney and give a head-start to your career in Python Programming!

Multiline Comment in Python

Unlike some programming languages that support multiline comments, such as C, Java, and more, there is no specific feature for multiline comments in Python. But that does not mean that it is totally impossible to make multiline comments in Python. There are two ways we can include comments that can span across multiple lines in our Python code.

  • Python Block Comments: We can use several single line comments for a whole block. This type of comment is usually created to explain the block of code that follows the Block comment. Python Block comment is the only way of writing a real comment that can span across multiple lines. It is supported and preferred by Python’s PEP8 style guide since Block comments are ignored by Python interpreter or parser. However, nothing is stopping programmers from using the second ‘non-real’ way of writing multiline comments in Python which is explained below.
  • Using Docstrings: Docstrings are largely used as multiline comments in Python by many programmers since it is the closest thing to having a multiline comment feature in Python. While it is not wrong to use docstrings when we need to make multiline comments, it is important to keep in mind that there is a significant difference between docstrings and comments. Comments in Python are totally ignored by the Python Interpreter, while docstrings, when used inside the Python function, can be accessed at the run time.

Career Transition

Non-Tech to IT Associate | Career Transformation | AWS Certification Course - Intellipaat Reviews
Non Tech to DevOps Engineer Career Transition | Intellipaat Devops Training Reviews - Nitin
Upskilled & Got Job as Analyst After a Career Break |  Data Science Course Story - Shehzin Mulla
Successful Career Change after Completion of AWS Course - Krishnamohan | Intellipaat Review
Got Job Promotion After Completing Artificial Intelligence Course - Intellipaat Review | Gaurav
Intellipaat Reviews | Big Data Analytics Course | Career Transformation to Big Data | Gayathri
test1= 7 * 2
type(test1)
”’
line one
line two
line three
”’
Output:
14
line one
line two
line three

This brings us to the end of this module in Python Tutorial.  The next module highlights Python data types. Let’s meet there!
Further, check our Python course and prepare to excel in career with our free Python interview questions for freshers listed by the experts.

Course Schedule

Name Date Details
Python Course 30 Mar 2024(Sat-Sun) Weekend Batch
View Details
Python Course 06 Apr 2024(Sat-Sun) Weekend Batch
View Details
Python Course 13 Apr 2024(Sat-Sun) Weekend Batch
View Details