In this article, you’ll learn about Comments in Python, their types and you will also learn about the different ways to write multiline comments in Python with examples.
So, Let’s Get Started!
Before diving deep into what actually Python comments are, or the different types of comments in Python,
I have a question for you,
And, The question is – Why do we need comments in Python?
Why do we need comments in Python?
If you know the answer, then feel free to skip this portion.
You can directly jump into the succeeding topics.
And, If you don’t know
Let’s find – The answer of why.
When you or someone else has written a short code (program). Then, It is much easier to read, understand or analyze the purpose of code, that what it is doing, or why.
But, Hold on!
What I mean by short code –
I mean a code which consists of few lines and does not contain any complicated syntax.
Such as – Few Assignment statements ( i.e., assigning values to variables) or few print statements which are doing some simple manipulation (calculation).
Think about a scenario in which you are writing a bigger code, which consists of so many lines, so many functions, and many more…
If you are working on a project or building some application.
Then, Of course your code will have lot of statements.
And, it is obvious that the code get more complicated, they get more difficult to read and understand.
Some time you write a code for let’s say 10 lines, 15 lines or maybe sometime it goes let’s say 100 to 1000 lines.
You don’t even write one file, so for a big project of course you’ll be writing multiple files, maybe 10 files 20 files and even more,
Now, What happens ?
When you got a code for the first time and if you’re writing it then everything goes well, everything is working.
After some time – let’s say one week or after a month, You will not remember what you did last time and why you have written that specific function.
Not just you, Let’s say – If you are working in a group or maybe if you’re working on a project for your company,
And, after some time someone else will working on that project then they need to understand what you did, and that’s where comments comes into picture.
For this reason, it is a good idea to add notes to your programs to explain in simple English what the program is doing. These notes are called Comments.
What are Comments in Python?
>>> Comments are notes or information in your program to explain in simple English what you have done in a program or what a certain section of program is doing.
>>> They are non-executable lines (non-executable statements) in your program which are ignored by the Python interpreter and it has no effect on the execution of the program.
Comments does not affect the outcome of a program.
A comment in Python starts with the hashtag symbol (
#). Hash tag (#) is also called as pound symbol.
Advantages of adding Comments
- Comments are added with the purpose of making the code easier for humans to understand.
- It makes the code more readable and understandable.
- It helps us to more easily find errors,
- It allows us to improve the code later on, and to reuse it in other applications or projects as well.
- To include external resources (Links).
- It can also be used to prevent the execution of some lines or code while testing other blocks of code.
Types of Comments in Python:
There are two types of comments in Python.
1. Single-line comments
2. Multi-line comments
1. Single-line Comments:
We can write single-line comments in Python just by putting hash tag (
#) at the beginning of a line, and is followed by text that contains further explanations.
Let’s understand clearly with the help of examples:
# This is an example of single-line comment in Python Print("Understanding comments with examples")
Understanding comments with examples
In the above example, the comment appears on a line by itself. And, it is at the top of the actual executable line.
You can also put comments at the end of a line:
x = 6 # distance in meter. print(x)
It means: Everything from the
# to the end of the line is ignored — it has no effect on the execution of the program.
2. Multi-line Comments:
Python does not have any special syntax for writing multi-line comments.
Commenting multi-line in Python is as simple as to put (
#) at the beginning of each line.
# This is an # example of # multiline comment in Python x = 2 y = 7 print(x + y)
It simply means that, Each line is considered as a single line comment, and all of them are ignored by the interpreter.
Another way to write multi-line comments in Python is to use triple-single quotes (”’) or triple-double quotes (“””) at the beginning and end of the multi-line string (i.e., sequence of characters).
Python interpreter treats triple quotes as a delimiter, so it will not execute the string inside these triple quotes.
When the comment text does not fit into one line.
If you want to comment out a paragraph then this type of commenting method is useful.
''' This is an another way to write multiline comments in python ''' print("Hello User")
In the above example, We can use either a triple-single quote (”’) or a triple-double quote (“””).
How do You Write a Good Comment?
Comments are most useful when they document non-obvious feature of the code.
It is full of common sense to assume that the reader can figure out what the code does; it is more useful to explain why.
This comments is unnecessary with the code and useless:
s = 4 # assign 4 to s
Let’s take a look at another example:
This comment contains useful information that is not in the code:
s = 45 # speed in meter/second.
Good variable names can reduce the need for comments, but long names can make complex expressions hard to read.
Keep Learning 🤗