Python Comments are non-executable statements and ignored by the interpreter and are not executed as part of the program. Comments help us to describe and explain the python program or code and to make it more readable. Python ignores everything after the hash mark up to end of the line.
How to Use Python comments to Write Better Code?
When writing code, it’s essential to make it as clear and readable as possible. So that other developers and including us to understand the python code when they refer. One way to achieve this is by using comments. In Python, comments are lines of text that are not executed as part of the program but provide information about the code. They are used to explain the code, make it easier to understand, and help other developers who might work on the same code in the future.
Syntax
In Python, comments start with the hash symbol (#) and continue until the end of the
As you can see, comments can appear on their own line or at the end of a line of code.
Types of Python Comments:
There are three types of python comments.
- Single line Comments
- Multiline Comments
- Docstring Comments
Single line Comments
Single line comment starts with the hash sign (#) symbol. Python compiler and interpreter ignores everything after the hash mark up to end of the line.
# This is a comment and not to execute
You can also add single line comments after the executable code. Here is a sample code for your reference.
Print(“Hi, Welcome to PythonPL blog”) # Display message
Multiline Comments
Multi-line comments can be created by enclosing the text between triple quotes (“””).
""" This is a multi-line comment Python Comments are used for Documentation purpose. Comments are helpful for the developers to understand python code. Comments are-non executable program code """
OR
You can also write like this syntax for multi-line comments.
To add a multi-line comment, you must insert a hash tag (#) for each line:
# This is a multi-line comment # Python Comments are used for Documentation purpose. # Comments are helpful for the developers to understand python code. # Comments are-non executable program code
Docstring Comments
Python docstring comment is the string literals. It contains triple quotes that are mentioned in the next line of function. It is used to document Python code in modules, functions, classes, and methods. We used to describe modules, functions, classes, and methods In the Python program.
def ExFunction(a, b): """"""Specify first and second values and add """""" First_Val=2 First_Val=4 return a+b
Best Practices for using Python Comments:
Here are some best practices to follow when using Python comments:
1. Be concise: Keep your comments brief and to the point. Avoid writing lengthy explanations that make the code harder to read.
2. Explain why, not what: Instead of explaining what the code does, focus on why it does it. For example, explain the reason behind a design decision or a particular algorithm.
3. Use clear language: Write comments in clear, concise language that everyone on your team can understand. Avoid using technical jargon or acronyms that may not be familiar to everyone.
4. Update comments as needed: Make sure to update comments when you modify the code. Comments that no longer reflect the code can be misleading and confusing.
5. Use consistent formatting: Use consistent formatting for comments throughout your codebase. This can include how you format multi-line comments or how you place single-line comments.
Conclusion:
Python comments are a powerful tool for making your code more readable, maintainable, and understandable. By following the best practices outlined in this post, you can use comments to improve your code’s quality and make it easier for yourself and other developers to work with. Remember to use comments wisely, keeping them concise and informative, and updating them as needed. With these tips in mind, you can write Python code that’s easy to understand, even for those who are not familiar with your codebase.
Instructions to execute python program
You can refer the following link for the step by step instructions.