Getting Started with Python Comments: A Guide to Clean Code”

Per aspera ad astra
3 min readJan 29, 2023

--

Comments are a crucial part of writing clean, organized, and easy-to-read code. In this lesson, we’ll cover the basics of Python comments and how to effectively use them in your code.

What are Python Comments?

In Python, comments are lines of text that are ignored by the interpreter. They are used to add explanations and clarifications to the code, making it easier for others to understand and maintain.

There are two types of comments in Python: single-line comments and multi-line comments.

Single-line comments are created using the # symbol and can be used to add a brief explanation or note to a single line of code. Here's an example:

# this is a single-line comment
x = 5 # this is also a single-line comment

Multi-line comments are created using three quotation marks (either single or double) and are used to add longer explanations or notes to the code. Here’s an example:

"""
This is a multi-line comment.
It can span multiple lines and is often used to add
detailed explanations or notes to the code.
"""

Why Use Python Comments?

There are several reasons to use comments in your code, including:

  1. Improving code readability: Comments help to clarify the purpose of code and make it easier for others to understand and maintain.
  2. Documenting code: Comments can be used to document the code, including information such as the author, date, and purpose.
  3. Debugging: Comments can be used to temporarily disable parts of the code while debugging.
  4. Keeping track of changes: Comments can be used to keep track of changes made to the code and to describe why certain changes were made.

How to Use Python Comments Effectively

Here are some best practices for using comments effectively in your code:

  1. Write clear and concise comments: Keep comments brief and to the point. Avoid writing lengthy or redundant comments.
  2. Use meaningful names: Use descriptive variable and function names to help make your code self-documenting. This will make comments less necessary.
  3. Comment only when necessary: Don’t overuse comments. If the code is self-explanatory, there may not be a need for a comment.
  4. Comment the why, not the what: Explain the purpose of code, not just what it does. This makes it easier to understand the reasoning behind the code.
  5. Keep comments up-to-date: If you make changes to the code, be sure to update the comments to reflect these changes.

Example

Here’s an example of a Python script that uses comments effectively:

# calculate the average of a list of numbers
def average(numbers):
# sum the numbers
total = sum(numbers)
# divide the total by the number of numbers
result = total / len(numbers)
# return the result
return result
# test the average function
numbers = [1, 2, 3, 4, 5]
avg = average(numbers)
print("The average is:", avg)
# output: The average is: 3.0

In this example, comments are used to explain the purpose of the code and to clarify the steps being taken in the average() function.

In conclusion, comments play a crucial role in making your code more readable and understandable. By adding comments, you can make your code easier for others to understand, as well as make it easier for you to revisit your code later on. As a best practice, you should always aim to add comments to your code to ensure it is well-documented and easy to understand. Now that you have a solid understanding of how to add comments to your Python code, you can start adding comments to your own projects and make your code even better!

--

--