Comments in Python
Comments are used to explain code and make programs easier to understand. Python ignores comments while executing the program.
💡 Writing good comments helps others (and your future self) understand code faster.
Single-Line Comments
Single-line comments start with the # symbol.
# This is a comment
print("Hello Python") # This prints a message
Multi-Line Comments
Python does not have official multi-line comments,
but we can use multiple # symbols.
# This is line one
# This is line two
# This is line three
print("Python Comments")
Docstrings
Docstrings are used to document functions, classes, and modules. They are written using triple quotes.
def add(a, b):
"""This function adds two numbers"""
return a + b
print(add(3, 4))
Why Use Comments?
- Improve code readability
- Explain complex logic
- Help in debugging
- Useful for teamwork
Common Mistakes
❌ Writing unnecessary comments
❌ Commenting obvious code
❌ Forgetting to update comments after changes
❌ Commenting obvious code
❌ Forgetting to update comments after changes
Practice Exercises
- Write a program with at least three comments
- Create a function and add a docstring
- Explain a calculation using comments