Python Comments
What is Comments in Python?
In Python, comments are explanatory notes within the code ignored by the Python interpreter and do not affect the execution of the program.
- Comments are used to provide explanations or information about the source code.
- Comments are used to comment out sections of code temporarily without removing the code entirely.
- Comments are not included during the code execution.
Comments are of two types: single-line comment and multi-line comment
How to write single-line comments in Python
Just add a hash (#) at the beginning of the line to write a single-line comment.
Everything after the hash (#) on that line is considered a comment.
Here is an example of single-line comment of Python:
Example of Single-line Comment
Output of the above example
Welcome to Python Single-line Comment Tutorial!
Look! The output of the code does not include the comments. Even commented lines do not affect the execution of the code.
How to write multi-line comments in Python
Python doesn't have a specific syntax for multi-line comments, but you can use single or double triple-quotes (''' or """) to create multi-line strings.
Anything between single or double triple-quotes (''' and ''' or """ and """) is treated as a comment (though this is technically string).
Here is an example of multi-line comment of Python:
Example of Multi-line Comment
Output of the above example
Welcome to Python Multi-line Comment Tutorial!
Look! The output of the above code does not include the comment. Even the commented lines do not affect the execution of the code.
Python Compiler For Live Practice
Practice the above Python code in the following Python Editor. Write python code with some comments in the following code editor and click Execute button. Output will be displayed in Python Code Output section.
Python Editor