Introduction to Computing

Comments in Python Source Code

Almost all programming languages allow the users to add comments inside the source code to help understand the functionality of the source code.

Python comments start with a ‘#’ symbol. They will be ignored by the Python interpreter and will not be executed while the program is running.

# Python comments example, yes this line is a comment.
print("This program is demonstrating the effects of comments")
# print("This line will not be executed, because it is a comment too")
print("This statement will also be executed up to the comment at the end of this line") # This is another comment 

Unlike Java, C or C++, Python does not support multiline comments, however you can use the multi-line docstrings (starting and ending with a triple ") to comment out a group of successive lines in your Python source codes. But bear in mind that they may appear in your documentation if you generate it from your code automatically using a documentation tool.

# Python comments example, yes this line is a comment.
print("This program is demonstrating the effects of comments")
"""
print("This line will not be executed, because it is inside a docstring")
print("This line will not be executed, because it is inside a docstring too")
print("This line will not be executed, because it is inside a docstring too")
"""
print("This statement will also be executed up to the comment at the end of this line") # This is another comment