Python Comments
1. What are Comments in Python?
Comments are notes written inside the code for humans.
Python does not run comments.
Comments help to:
-
Understand code easily
-
Explain logic
-
Remember code later
Simple Example
# This line prints a message
print("Hello Python")
Python runs only:
print("Hello Python")
2. Single-Line Comments
Single-line comments start with #.
Example
# This is a single-line comment
print("Python is easy")
You can also write comment after code.
print("Hello") # This prints Hello
3. Multi-Line Comments (Docstrings)
Python does not have special multi-line comments,
but we use triple quotes to write multi-line notes.
Example
"""
This program shows
how Python comments work
"""
print("Welcome")
These are called docstrings.
4. Why Use Comments?
Comments make code:
✔ Easy to read
✔ Easy to understand
✔ Easy to fix later
Example Without Comment
a = 10
b = 20
print(a + b)
Example With Comment
# Adding two numbers
a = 10
b = 20
print(a + b)
The second one is better.
5. Comments for Explaining Steps
Example
# Take input from user
age = int(input("Enter age: "))
# Print next year age
print(age + 1)
6. Comments to Disable Code (Temporary)
Sometimes we don’t want a line to run.
Example
print("Start")
# print("This line will not run")
print("End")
Output:
Start
End
7. Good Comment Writing Tips
✔ Keep comments short
✔ Write simple English
✔ Explain why, not obvious code
Bad Comment
x = 10 # x is 10
Good Comment
x = 10 # Store default value
8. Common Beginner Mistakes
❌ Forgetting #
This is a comment
print("Hello")
✔ Correct:
# This is a comment
print("Hello")
❌ Writing Too Many Comments
# Print hello
print("Hello") # This prints hello
One comment is enough 🙂
9. Simple Practice Examples
Example 1: Commented Program
# This program prints user name
name = input("Enter your name: ")
print("Hello", name)
Example 2: Math with Comments
# Take two numbers
a = 5
b = 10
# Print sum
print(a + b)
10. Summary (Python Comments)
✔ Comments are for humans
✔ Python ignores comments
✔ Use # for single-line
✔ Use """ """ for multi-line notes
✔ Comments make code clean
📘 Perfect for Beginner eBook
This chapter is perfect for:
-
Python beginner books
-
School & college students
-
Self-learners
If you want next, I can write:
-
Python Variables (easy)
-
Python Input & Output
-
if–else statements
-
Loops (for, while)
-
Bangla + English version
Just tell me 😊
