Here you will learn about comments in Python with its examples.
Comments in Python are used by the developer to provide, give or write some information about some line of code in Python program.
Comments are ignore by the Python interpreter, therefore comments helps us in debugging the program.
Comments in python, also helps in reviewing the python code later.
There are two types of comment available in Python. That is,
Single line comments are the comments that can be written in only one line with the help of commenting code that is used to write at the beginning of comment.
All characters after hash (#) sign referred as comment (single line comment), up to the physical line end. Here is an example of single line comment in Python:
# Python Basic Syntax - Example Program # We are comments print("Hello World, I am Python"); # i am also comment
This will produce the following output:
Comments that can be written in more than one line with the help of single commenting code is known as multiline comment in Python.
Multiline starting commenting code is ''' and ending with same, that is '''.
Let's follow the example given below to understand about multiline comment in Python completely.
''' This is multiline comment ''' ''' Multiline comment starts with \''' and ends with \''' ''' print("Hello Python,\nThis is multiline comment example");
Note - In the above example of multiline comment, \ is used to skip ending the multiline comment. Therefore only use ''' before writing the multiline comment and after writing the comment, close with same, that is, '''.
In other word, multiline comments are used inside triple quotes.
Here is the screenshot of the Python file where the above example is written, that is codescracker.py
If you run the above Python code, then you will see the following output on your output screen.
As you can see that the following line:
''' This is multiline comment ''' ''' Multiline comment starts with \''' and ends with \''' '''
written in the above example is not printed on output screen after running the Python program. This happens as the above line of text or code is multiline comment.