Comments in python or any programming language are assumed to be non-readable parts of any program by the compiler/interpreter.
Comments are used to explain any code, make the code more readable, and prevent execution when testing code.
There are two types of comments:
- Single-Line Comments
- Multi-line Comments
1. Single-Line Comments in Python
Single line comments start with “#” and can be used anywhere in a program, see the example below
Example
# This is a single line comment in Python print("We are learning comments in Python") print("Nothing") # This is an inline comment
Output
We are learning comments in Python Nothing
2. Multi-Line Comments in Python
In python, single quotes(‘) and double quotes(“) represents the same meaning.
If you put anything inside a multiline string(triple quotes) anywhere in your code then that will be called multiline comments, see the example below.
Example
print("Example of multiline comments") """This is a multiline comment in python """ '''This is also a multiline comment in python with the help of single quotes '''
Output
Example of multiline comments