Python Comments
Python comments are used to explain code and make it more readable. They are ignored by the Python interpreter during execution. There are two main ways to write comments in Python, as shown in your code.
Source Code
|
|
Run Output
Code Understanding
#A simple addition program
#Add two values
These are two single line comments. Many python programmers prefer putting many single line comments instead of multi-line comments.
a=1 #value 1
b=2 #value 2
These two lines have individual in-line comments which follow the # sign.
“””
These are comment lines
line 1
line 2
“””
This is a multi-line comment block. This is basically a multi-line string which is not assigned to a variable. One can use three single quotes also instead of three double quotes to give multiple line comments.
print(a+b) #result
This is also a inline-comment.
Notes
- As shown in this code, the comments must be relevant to the action being taken from the documentation. For e.g. for print line #result is a better comment than #print a+b
- Too much of commenting is also a kind of distraction. For e.g. in this code comments #value 1 and #value 2 are actually not required. We have done here for explanation purpose only.
- A single line comment may not start from the first column. It can be placed after spaces or tabs.
- The python interpreter ignores everything it encounters from # symbol to end of line.
Common Errors
- Actually python supports just one comment character which is #, that is why doing a multi-line type of commenting using “”” within a statement will fail. For e.g. a=10+ “””comment””” 5 is not a valid statement in python. It’s counterpart in c++ like a=10+/*comment*/5; would be quite okay.
Suggested Filename(s): python-comments.py
Created: 29-Jul-2026 18:13 | Updated: 30-Jul-2026 19:53|
