Python Comments
Comments are notes written inside Python code to explain how logic works, document assumptions, or temporarily disable lines during testing. Python ignores comments completely when executing a program, so they have zero effect on runtime output.
Single-Line Comments
Single-line comments start with a hash symbol (#). Everything from # to the end of that line is treated as a comment.
# This is a single-line comment
print("Hello, Python!")
price = 199 # Inline comment explaining the variable
print(price)
Multi-Line Comments
Python does not have a dedicated multi-line comment token like /* ... */ in C or Java. Instead, developers either use consecutive # lines or triple-quoted strings ("""...""" or '''...''') when not assigned to a variable or used as a docstring.
# Step 1: Set user details
# Step 2: Print a greeting message
user = "Riya"
print(f"Welcome, {user}!")
"""
This multi-line string is not assigned to any variable,
so Python skips it during execution.
"""
Best Practices for Writing Comments
- Explain Why, Not Just What: Focus on why a non-obvious condition or workaround exists rather than repeating obvious code.
- Keep Comments Updated: Outdated comments are worse than no comments—update them whenever you change the code.
- Avoid Commented-Out Code Clutter: Remove old commented blocks once features are finished so your file stays clean.