[Link].
in
Python Comments
• Comments in Python are used to explain code and make it more readable.
• They are ignored by the Python interpreter.
• Useful for documentation and debugging purposes.
• Comments can be used to prevent execution when testing code
1. Types of Comments
a. Single-line Comments
• Begin with # and extend to the end of the line.
b. Multi-line Comments (Block Comments)
• Python does not have a specific multi-line comment syntax.
• Conventionally, multiple # symbols are used.
• Alternatively, triple quotes (''' or """) can be used, though they are
actually treated as docstrings if not assigned to a variable.
1
[Link]
c. Docstrings (Documentation Strings)
• Enclosed in triple quotes (''' or """). Used for function, class, or
module documentation.
• Docstrings can be accessed using inbuilt help()function:
Note:
We will learn more about docstrings in the Docstrings topic.
2. Best Practices for Writing Comments
• Keep comments concise and meaningful.
• Avoid redundant comments.
• Use comments to explain why rather than what.
• Follow PEP 8 guidelines for writing clean comments.
• Use docstrings for module, class, and function documentation.