Commenting on Python is an operation that in some ways is very different from other languages, but it is quite easy to get used to this new way of inserting the explanatory text in our codes.
In Python there are basically two ways to comment on a program:
- single line of comment
- multiple line of comment
The single comment line is used to insert a short comment (or for debugging), while the multiple comment line is often used to describe something much more detailed.
Let’s see then some explanatory examples to better understand the concepts introduced up to now. Let’s start with the single line of comment:
print ("This is not a comment") #print ("This is a comment")
Then when the interpreter encounters the symbol # (hash) ignores everything following the symbol until the end of the line. We could also write like this:
print ("This is not a comment") # Printing a text string
As for the multiple comment line instead we will use the symbol “‘, let’s see how:
" ' print ("This is not a comment") print ("Additional comment line") " ' print ("This is not a comment")
The comments are a useful resource for the programmer because they allow us to insert very valuable explanatory text in the optics reusability, but they are also particularly effective in the code debugging phase where the insertion of text strings helps in identifying possible bugs in our program.