Comments In Python
Comments are texts in code which are ignored by the interpreter or the compiler during program execution. They are written to explain the code functionality and also to improve code readability.
Syntax
# Hello i am a comment
In python you can simply write a comment by writing a # then your comment. Above is called a single line comment.
Example
# Below code prints hello world
print("Hello world")
print("Hi") # Output: Hi
Multi Line Comments
'''
This is a multi-line comment.
I am another line
'''
print("I am runnable")
In python to write multi line comment you can write using triple quotes(”’ or “””). It is mostly used for writing big comments.
Why Write Comments?
- Clarity: Comments make code clearer, explaining its purpose and logic.
- Understanding: They help others (and yourself) understand the code’s functionality.
- Readability: Well-placed comments improve code readability, especially for complex parts.
- Debugging: Comments helps in debugging by allowing you to temporarily disable code.
- Instructions: They can include instructions or notes for users or collaborators.
Guidelines for Effective Comments:
- Keep comments concise and to the point.
- Avoid redundant comments that merely restate the code.
- Update comments when modifying code to ensure accuracy.
So this was everything about writing comments in python.