Comments In Python
If it comes to simplifying the user's work, then it is comments. After commenting any code
or line, the Python interpreter does not execute that code or line. # is used to comment a
program/line in the Python language. By adding # before a line , that line becomes a comment.
Python supports three primary types of comments based on how they are written and applied :
1. Single-Line Comments : Single-line comments are used for brief, quick
explanations of a specific line or expression. They begin with the hash or pound symbol (#).
Anything written after the # on that exact line is treated as a comment.
# Initialize the user session counter to zero
session_counter = 0
2. Inline Comments An inline comment is a single-line comment placed on the exact same line as a code statement, separated by at least two spaces. It is highly effective for explaining specific or tricky calculations on the spot.
final_price = base_price * 1.18 # 1.18 represents 18% GST tax rate
3. Multi-Line Comments : Python does not feature a dedicated built-in syntax symbol for multi-line comments (unlike /* ... */ in C++ or Java). However, developers use two highly accepted methods to write long, multi-line explanations :
Method A: Consecutive Hash Symbols# This block of code calculates the compounded interest rate
# based on the quarterly financial returns of the user account.
# Ensure the interest rate variable is non-negative.
"""
This is treated as a multi-line comment.
The interpreter will read it but won't execute it
because it is not assigned to any variable.
"""
Key Rules & Best Practices for Writing Comments
Writing too many comments or writing them poorly can ruin your codebase. Follow these industry
standards :
1. Avoid the Obvious : Do not write comments that state exactly what the code
is doing.
Bad: x = 5 # Assign 5 to x
Good: x = 5 # Maximum allowed login attempts before locking account
2. Keep Them Up to Date : A comment that contradicts the actual running code is
worse than no comment at all. Always update your comments when you modify the logic.
3. Code Block Alignment : : Comments must match the exact same indentation
level as the code block directly beneath them.
4. The Two-Space Rule for Inline : : Always leave at least two spaces between
the end of your code statement and the start of your inline # symbol.
Benefits of comments
1. Readability : Comments are very helpful for understanding a big program,
especially for students who start learning coding, comments are very helpful for them.
2. Future : Suppose that you write a 200 lines program and then after a few years,
if you see that program, you will not be able to understand some codes/lines of that program.
So comments are helpful in understanding a program in the future also.
3. Debugging : After finding an error in the program, it is helpful in separating some
part of the program.
4. Explanations : It is helpful in explaining big programs.
5. Different Person : Suppose that if you give your program to another person, then it
is helpful in explaining the program to him.