python comments
Comments in Python are hints and descriptions that we use to make our code more understandable. Comments in Python are used by the developer to write down his ideas while writing the code. To explain the underlying logic and reason for writing a particular line of code. Comments are completely ignored by the Python interpreter. They are only for programmers themselves or other developers to understand the code.
What is the use of comments in Python?
- Explanation of Python code.
- Make the code more readable.
- Make the code easy to understand by other programmers
- It helps remember why we used a certain command, method or function in the code
- Prevent execution when testing code.
Comment installation in Python
Comments in Python begin with a hash sign ( # ) and a white space character and continue to the end of a line.
Comments must be at the same indentation level as the code they are commenting on.
Example:
#This is a comment
print("Hello, Ahmed !")
Types of comments in Python
There are three types of writing a comment:
- Comment on a separate line
- Comment next to the code (on the same line)
- Multiline comment
_ separate line comment ( a single line commentundefined )
Example:
# create a variable
name = 'Ahmed Obaid'
_ Comment next to the code (on the same line)
We can also use the single-line comment with the code on the same line
Example:
name = 'Ahmed Obaid' # name is a string
_ Multiline comment in Python
Each line is treated as a separate comment. Example:
# This is a long comment
# and it extends
# to multiple lines
Another way to do this is to use triple quotes, either single ( ''' ) or even (""") .
These triple quotes are generally used for multiline strings. But if we don't assign it to any variable or function, we can use it as a comment.
The Python interpreter ignores strings that are not assigned to any variable or function.
Let's see an example,
''' This is also a
perfect example of
multi-line comments '''
How do you write good comments in Python?
Comments are an important part of the program. Thus, it is essential that we learn how to write good comments. Here are some of the characteristics that define good feedback.
- Make sure it is brief
- Write an easy-to-understand comment
- Write comments that describe the basic functionality of the code
- no undefined You write redundant comments
Use comments for debugging
If we get an error while the program is running, we can comment out the line of code that is causing the error instead of removing it. for example,
print('Python')
# print("Error Line )
print('Django')
Here, print("Error Line) was causing an error so we changed it as a comment. Now, the program runs without any error.
This is how feedback can be a valuable debugging tool.
Conclusion:
Writing good comments in Python will allow other programmers to easily read and understand your code.
If you have any questions? Leave it in the comments
Tags
Python python comments
Share page
About author
Ahmed Obaid
Hello, I am Ahmed Obaid, an Egyptian Arabic programmer. I would like to put my experiences learning Python on this site So that it will be a reference for you and me as well.
Sign up to leave a comment