Python Comments
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
Creating a Comment
Comments starts with a #, and Python will ignore them:
#This is a comment
print("Hello, World!")
Comments can also be added at the end of a line of code. Everything after the comment symbol () on that line is ignored by Python when the program runs.
#
print("Hello, World!") #This is a comment
Comments are not only used to explain code. They can also be used to temporarily disable code, preventing Python from executing it when the program runs.
#print("Hello, World!")
print("Cheers, Mate!")
Multiline Comments
Python does not really have a syntax for multiline comments.
To add a multiline comment you could insert a # for each line:
#This is a comment
#written in python
#more than just one line
print("Hello, World!")
Another way to write multi-line comments is by using triple quotes ( or ''').
"""
Python ignores multi-line strings that are not assigned to a variable, so you can place your comment inside a triple-quoted string. Although this is not the official way to create comments, it is sometimes used for longer explanations.
""" is also called Docstrings (Documentation Strings).
"""
This is a comment
written in python
more than just one line
"""
print("Hello, World!")