Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Imagine you are reading a notebook written by someone else.
The notebook contains many calculations, but there are no notes explaining why a calculation was done.
It may be difficult to understand.
Programming can be the same.
Code tells the computer what to do, while comments and documentation can help humans understand the code.
In this lesson, we will learn:
A comment is a note written inside your code for humans.
Python does not execute a normal # comment as a Python instruction.
For example:
The first line is a comment.
Python ignores it when executing the program.
The second line is actual Python code.
A single-line comment starts with:
Everything after # on that line is treated as a comment.
For example:
Comments make the code easier for another person to understand.
Comments can be useful for several reasons.
For example:
Output:
The commented-out line is not executed.
A comment can also be written on the same line as code.
For example:
Here:
is the Python code, and:
is the comment.
According to common PEP 8 style, an inline comment is normally separated from the code by at least two spaces.
Good:
Less readable:
A good comment should provide useful information.
For example:
This gives the reader useful context.
But this comment is not very useful:
The code already clearly tells us what is happening.
Good code should be understandable by itself as much as possible.
Compare:
with:
The second example is easier to understand even without comments.
This is called readable or self-explanatory code.
A docstring is documentation written inside a function, class, or module.
It is usually written using triple quotes:
For example:
The text:
is the function's docstring.
Docstrings explain what a function, class, or module is designed to do.
For example:
Someone reading this function can quickly understand its purpose.
Docstrings are especially useful when your program becomes larger.
Python allows us to access a function's documentation using:
For example:
Output:
You can also use:
Python will display information about the function.
This difference is important.
| Feature | Comment | Docstring |
|---|---|---|
| Starts with | # | Usually """...""" |
| Main purpose | Add notes for developers | Document functions, classes, or modules |
Can be viewed through __doc__? | No | Yes |
| Used as documentation? | Sometimes | Yes |
| Example | # Calculate total | """Calculate the total price.""" |
Docstrings are commonly used with:
For example:
Later in the course, when we learn functions and classes in more detail, you will use docstrings more often.
Python does not have a special /* ... */ comment syntax like some other programming languages.
If you want a comment to span multiple lines, simply use # on each line:
This is a real comment.
You may sometimes see this:
It is important to understand that triple quotes create a string, not a special comment syntax.
Triple-quoted strings are especially useful for docstrings.
For example:
Here, the triple-quoted text is a proper docstring.
# for comments.
Use """...""" for docstrings.Good programmers don't try to explain every single line with a comment.
Instead, they aim to write code that is already easy to understand.
For example, this is clear:
You don't need:
The variable name already explains the purpose.
But a comment can be useful when the reason is not obvious:
The comment explains why, not simply what.
Follow these basic rules:
Write comments that provide useful context.
Avoid writing a full paragraph when one sentence is enough.
If the code changes, update the related comment.
Bad:
If the code later changes to apply the discount to everyone, the old comment becomes misleading.
Prefer:
over:
Clear names reduce the need for unnecessary comments.
For example:
PEP 8 is Python's style guide.
It contains recommendations for writing clean and readable Python code.
For comments, some useful guidelines include:
You can think of PEP 8 as:
You don't need to memorize the entire PEP 8 document.
We will gradually follow its useful recommendations throughout this course.
| Do | Don't |
|---|---|
| Write useful comments | Explain every obvious line |
| Keep comments short | Write unnecessary paragraphs |
| Update old comments | Leave misleading comments |
| Use clear variable names | Use meaningless names everywhere |
| Use docstrings for functions | Use comments instead of proper documentation |
Use # for normal comments | Treat triple quotes as a special comment syntax |
Let's look at a small program:
Notice the different types of explanations:
The comment gives a quick note.
The docstring documents what the function does.
In this lesson, you learned:
#..__doc__.help() can also display documentation./* ... */ multiline comment syntax.# for normal comments.A.
B.
C.
D.
# This is a commentA.
B.
C.
D.
A.
B.
Create a file named:
Then write:
Run the program:
Now make three small changes.
Add a comment above the marks variable explaining what it stores.
Change the function's docstring to:
Add another function:
Then run:
Expected output:
🎉 You have now practiced both comments and docstrings.
In the next lesson, we will continue your Python learning journey with Variables Introduction and Rules (2: Variables).
👉 Continue to Next Lesson: Variables Introduction and Rules →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Syntax & Code Structure | Variables Introduction and Rules |
Test your understanding of this lesson with 1 questions. Each question has one correct answer.