Comments are lines in a Python program that are ignored by the Python interpreter during execution. They are written to explain the purpose of the code, improve readability, and help developers understand the program more easily.
Comments are not executed as part of the program, which means they do not affect the output.
Writing meaningful comments is considered a good programming practice, especially when working on large projects or collaborating with other developers.
Note
Comments are written for humans, not for Python. They help developers understand the code without changing how the program works.
Why Do We Use Comments?
Comments make programs easier to read and maintain. Even if you write the code yourself, comments can help you remember the purpose of your code when you revisit it after weeks or months.
Some common uses of comments are:
1.Explaining how a piece of code works.
2.Describing the purpose of variables, functions, or classes.
3.Adding notes or reminders for future improvements.
4.Temporarily disabling code while testing or debugging.
5.Making code easier for other developers to understand.
Real World Scenario
Imagine you are working on a banking application with a team of developers. A comment explaining why a particular calculation is performed can save other developers a lot of time when they need to modify or debug the code.
Creating a Single Line Comment
In Python, a single line comment starts with the # symbol.
Everything written after the # on that line is ignored by Python.
Output
The comment is skipped during execution, so only the print() statement produces output.
Comments at the End of a Line
A comment can also be written after a statement on the same line.
Everything after the # symbol is treated as a comment.
Output
Inline comments are useful for giving a short explanation of a specific line of code.
Using Comments to Disable Code
Comments are often used to temporarily prevent Python from executing a line of code.
This technique is helpful while testing or debugging a program.
Output
Instead of deleting the code, you can comment it out and enable it again whenever needed.
Pro Tip
Commenting out code is useful during debugging because you can temporarily disable specific lines without permanently removing them.
Multi Line Comments
Python does not have a special syntax for multi line comments like some other programming languages.
The recommended way is to place the # symbol at the beginning of every line.
Output
This approach is clear, readable, and follows Python's recommended coding style.
Triple Quotes as Multi Line Comments
You may sometimes see triple quoted strings used to write notes across multiple lines.
Output
Although this works in many situations, these are actually multi line strings, not true comments.
Python simply ignores them because they are not assigned to a variable or used anywhere.
Caution
Triple quoted strings are not official multi line comments. For regular comments, using # on each line is the recommended and widely accepted practice.
Writing Good Comments
Good comments explain the purpose of the code rather than describing every obvious statement.
Consider the following example.
Poor comment
Better comment
The second comment explains why the value is being printed, making it more useful.
Bad vs Good Comments
Bad Example
The code is already simple, so the comment adds no useful information.
Good Example
The comment provides meaningful context that is not immediately obvious from the code.
Common Mistakes
Forgetting the # Symbol
Incorrect code
Python treats the first line as code and produces an error.
Correct code
Writing Too Many Comments
Avoid commenting every single line when the code is already easy to understand.
Poor example
Better example
Meaningful comments improve readability, while unnecessary comments make code harder to read.
Using Outdated Comments
If the code changes, update the comments as well.
Incorrect example
The comment no longer matches the code, which can confuse developers.
Stop
Incorrect or outdated comments can be more harmful than having no comments at all.
Best Practices
Write comments only when they add useful information.
Keep comments short and meaningful.
Explain why the code exists instead of describing every obvious line.
Update comments whenever the related code changes.
Avoid writing unnecessary comments for simple statements.
Follow a consistent commenting style throughout your project.
Great Job
You have learned how to create single line comments, write comments at the end of a statement, temporarily disable code, and document programs using meaningful comments in Python.
Exercise
1.Write a program with a single line comment.
2.Add an inline comment after a print() statement.
3.Comment out a print() statement and observe the output.
4.Write a program using three consecutive single line comments.
5.Create a short program and add meaningful comments explaining its purpose.
6.Write a multi line note using triple quotes and observe that the program still runs correctly.
Challenge
Create a Python program for a student's report card.
Requirements
1.Add a comment describing the program.
2.Store the student's name and marks in variables.
3.Print the student's details.
4.Comment out one print() statement.
5.Add an inline comment explaining another statement.