How to Comment in Python within code?
To enhance the readability of any complex code, comments are added to the code. These lines do not affect the program and are added to explain the code. Comments also help other users who want to use the code. To make complex code clear and simple comments are used. Let’s learn how to use the comment in Python.
How to Use Comments in Python
In Python, we can use the hash (#) sign to start a comment. The comment begins from the hash (#) sign and continues until the end of the line.
In this case, the first line of the code does not execute because it is preceded by a hash(#) sign.
How to Comment Multiple Lines in Python
Comment multiple lines in python we can use 3-times double quote, and that will be considered as comment.
""" print(we are using triple double quote to comment) multiple line comment """
In this case, nothing will be printed because we are printing inside 3-times double quote.
#comment line 1 print("hello world") #comment line 2
In the above code first and the last line will not be printed as they are preceded by hash(#) symbol
""" print(we are using triple double quote to comment) multiple line comment """ print("hello world")
We can see lines inside 3-times double quote are not printed because those lines are used as a comment and outside double quotes will be normal python code.
In a for loop that iterates over a list, using comments may look like this:
# Define pokemon variable as a list of strings pokemon = ['Pidgey', 'Pikachu', 'Arceus', 'Entei.', 'Scizor', 'Latios'] # For loop that iterates over pokemon list and prints each item in pokemon list for pokes in pokemon: print(pokes)
Pidgey Pikachu Arceus Entei Scizor Latios
Including appropriate comments that are useful can make it easier for others to understand your program and make the value of your code more obvious.