Last modified: Aug 22, 2026
Python Comments: A Beginner's Guide
Comments are an essential part of any programming language. They help you explain your code, make it more readable, and improve collaboration with other developers. In Python, comments are ignored by the interpreter, so they do not affect the execution of your program.
This guide will show you everything you need to know about writing comments in Python. We will cover single-line comments, multi-line comments, and docstrings. You will also learn best practices to make your code cleaner and more maintainable.
What Are Comments and Why Are They Important?
A comment is a note you leave in your code for humans to read. It explains the logic behind a specific line or block of code. When you revisit your code after a long time, comments help you remember what you were thinking.
Comments are also crucial for team projects. They allow other developers to understand your intentions without reading every single line. This saves time and reduces errors. Good comments make good code.
Python does not have a specific comment syntax for multi-line comments like some other languages. Instead, you use the hash symbol for single lines and triple quotes for multi-line strings, which act as comments.
Single-Line Comments with #
The most common way to comment in Python is by using the hash symbol (#). Anything after the # on the same line is considered a comment and will not be executed.
You can place a single-line comment on its own line or at the end of a line of code. This is perfect for short, quick explanations.
# This is a single-line comment
print("Hello, World!") # This comment is after the code
When you run this code, Python will ignore both comments and only print the string. The output is simple and clean.
Hello, World!
Use single-line comments to explain the "why" behind a complex operation. Avoid stating the obvious. For example, instead of writing # add 1 to x, write # increment counter for next iteration.
Multi-Line Comments: Using Triple Quotes
Python does not have a dedicated multi-line comment syntax. However, you can use triple quotes (''' or """) to create a multi-line string. If you do not assign this string to a variable, it acts as a comment.
This is useful for longer explanations that span multiple lines. It is also the standard way to write docstrings, which we will discuss later.
"""
This is a multi-line comment.
You can write as many lines as you want here.
It will not affect the program's output.
"""
print("Multi-line comment example")
When you execute this script, only the print statement runs. The triple-quoted string is ignored because it is not assigned to anything.
Multi-line comment example
While this works, many developers prefer to use multiple single-line comments for consistency. However, triple quotes are the official way to create docstrings, which are a special type of comment.
Docstrings: Comments for Functions and Classes
Docstrings are a powerful feature in Python. They are used to document modules, functions, classes, and methods. A docstring is a string literal that appears as the first statement in a definition.
To create a docstring, you use triple quotes right after the function or class definition. This helps users understand what the function does, its parameters, and its return value.
def greet(name):
"""This function greets the user.
Args:
name (str): The name of the user.
Returns:
str: A greeting message.
"""
return f"Hello, {name}!"
print(greet("Alice"))
Docstrings are not just for humans. You can access them using the __doc__ attribute or the built-in help() function. This makes your code self-documenting.
Hello, Alice!
You can also view the docstring by using help(greet) in the interpreter. This is extremely helpful for other developers who are using your code.
Inline Comments and Best Practices
Inline comments are placed on the same line as the code. They should be used sparingly and only when necessary. Too many inline comments can make your code cluttered and hard to read.
When you use inline comments, make sure they add value. Do not repeat the code. Instead, explain the logic or the reason behind a specific choice.
x = 5 # Initialize x with value 5
y = x * 2 # Double the value of x
print(y)
In the example above, the comments are not very helpful because they just restate the code. A better approach is to explain the purpose of the calculation.
10
Here are some best practices for writing comments in Python:
- Keep comments concise and to the point.
- Use complete sentences with proper grammar.
- Update comments when you update the code.
- Avoid commenting out code unless it's temporary.
- Use docstrings for all public functions and classes.
Following these practices will make your code more professional and easier to maintain.
Common Mistakes to Avoid
One common mistake is writing too many comments. If your code is simple and self-explanatory, you do not need to comment every line. This adds noise and makes it harder to spot important notes.
Another mistake is using comments to explain bad code. Instead of commenting on why the code is messy, fix the code. Clean code should be your first priority.
Also, avoid using triple quotes for regular multi-line comments if you are not creating a docstring. Some linters will flag this as a warning. Stick to single-line comments for clarity.
Conclusion
Commenting in Python is straightforward once you understand the syntax. Use # for single-line comments and triple quotes for multi-line comments or docstrings. Remember that comments are for humans, not machines.
Effective comments improve code readability and reduce bugs. They help you and your team work faster. Always aim to write clear, concise, and useful comments that explain the "why" behind your code.
Start practicing today by adding comments to your existing Python scripts. You will notice the difference immediately when you revisit your code later.