python comments
Python code can be explained by comments.
Comments may be used for the readability of the code.
Comments may be employed when testing code to prevent execution.
Creating a Comment
Python ignores comments that begin with a #
:
Example 1 :-
#This is a comment.
print("Hello, World!")
Output :-
Python will disregard the remainder of the line if you put a comment at the end of a line :
Example 2 :-
print("Hello, World!") #This is a comment.
Output :-
It's not necessary for a comment to be text that explains the code; it can also be used to Prevention Python from running code.
Example 3 :-
#print("Hello, World!")
print("Cheers, Mate!")
Output :-
Related Links
Multi Line Comments
The syntax for multi-line comments is not really available in Python.
To add a multiline comment, use #
for each line :
Example 1 :-
#This is a comment
#written in
#more than just one line
print("Hello, World!")
Output :-
You can also make use of a multiline string, which is not exactly what was intended.
As the literal strings the variable has not been assigned to are ignored by Python, you can insert a many-line string (three quotes) into your code.
Example 2 :-
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Output :-
Until the string is assigned to a variable, Python will read, ignore, but have made multiline comments about the code.
Related Links