Comments in Python
Comments in Python are used to provide explanations or notes within the code. They help developers understand the code and make it easier to maintain. Python ignores comments during execution, so they have no effect on the program's output.
Why Use Comments?
- Improve Readability: Comments make code easier to understand for others (or yourself in the future).
- Explain Logic: Helps to explain what a certain part of the code is doing.
- Temporary Disable Code: You can comment out code that you donβt want to run temporarily.
- Documentation: Provides important details about a function, module, or script.
-
Debugging: Debugging using comments means temporarily disabling some parts of the code by adding a # at the beginning. This helps in testing and fixing programs without deleting the original code.
Types of Comments
-
Single-Line Comment
In Python, a single-line comment starts with a
#
symbol. Anything written after the#
on the same line is ignored by the Python interpreter. It is used to explain a specific line or part of the code.π» Example:
# This is a single-line comment print("Hello, From ShikshaSanchar!") # This comment is after code
Output: Hello, From ShikshaSanchar!
π Explanation:
- The first line
# This is a single-line comment
is completely ignored when the program runs. It is only for human readers. - The second line has a print statement and after it, a comment is added on the same
line using
#
. - Adding comments next to the code is useful to quickly describe what that line is doing.
- The first line
-
Multi-Line Comments
Python does not have a separate syntax for multi-line comments like some other languages. But we can use triple quotes β either(
'''
or"""
) β to create block comments.π» Example:
''' This is a multi-line comment explaining the next part of the code. ''' print("Welcome to ShikshaSanchar")
Output: Welcome to ShikshaSanchar
π Explanation:
- Triple quotes are usually used for writing docstrings (documentation inside functions), but they can also act as multi-line comments.
- When placed outside any function or not assigned to a variable, Python will ignore them β just like a comment.
- This helps us explain large parts of code in one go.
Best Practices for Writing Comments
- Keep comments short and meaningful.
- Do not repeat the obvious (e.g.,
# Print Hello
aboveprint("Hello")
). - Update comments if the code changes.
- Use consistent style and grammar.
- Use comments to explain why something is done, not just what is done.
Example with Comments
In this program, instead of taking input from the user, we use predefined numbers and add them. Comments are added to explain each step clearly.
π» Program:
# This program adds two predefined numbers
# Defining two numbers
num1 = 8
num2 = 12
# Adding the numbers
sum = num1 + num2
# Printing the result
print("The sum is:", sum)
Output:
The sum is: 20
π Explanation:
- num1 and num2 are predefined with values 8 and 12.
- The + operator is used to add the two numbers.
- The print() function displays the sum on the screen.