How Not To Write Python Comments
Tips to improve the way you write and use comments
A Python comment is an explanation in the source code of a Python program. It doesn’t do anything besides being informative and is ignored by the Python interpreter. In this article, you will learn:
- what Python comments are,
- how to add comments to your Python code,
- what the common pitfalls are when writing comments.
1. What is a Python comment?
Let’s start by defining what a comment is exactly:
A comment is an explanation in the source code of a Python program. Comments are added with the purpose of making source code easier for humans to understand. They are ignored by the Python interpreter.
So, in short, a comment helps you and other readers of your code to better understand it.
2. A single-line Python comment
In Python, we create a comment by starting the line with the hash symbol: #. This symbol is known as the number sign, hash, or (in North American usage) pound sign. Whatever you call it, this is what it looks like:
# This is a single-line comment in Python
print('Hello')
# And here’s another one!