Learn How to Use Comments in Python Programming

Comments in Python
In programming, comments are statements ignored by the interpreter or compiler when the program runs. They are used to explain a specific block of codes or a line of code.
Comments can be used to improve the readability and overall understanding of a program.
It can also be used to prevent a specific block of code or line of code from executing.
How to Create Comments in Python Programming
Comments can be short (single-line comment) or long (multi-line comments).
Single-Line Comments
In python programming, short comments begin with the #
symbol. This means that python will not execute any line that begins with it.
- Python Example Code
#This is a program that prints "Hello, Archangel!" as an output.
print("Hello, Archangel!")
You can place a comment at the end of each line of code.
- Python Example Code
print("Hello, Archangel!") #This is a program that prints "Hello Archangel" as an output.
You can also comment on one or more codes. This will prevent the code from executing.
- Python Example Code
# print("Hello, World!")
print("Hello, Archangel!")
The output of the above programs will be:
- Output
Multi-Line Comments
Comments do not always have to be in a single line. Sometimes, you may want to write a paragraph describing a block of codes. This is called a multi-line comment.
There is no official syntax for a multi-line comment in Python programming. But, there are ways around it.
One way to implement multi-line comments in Python programming is the use of multiple short comments by inserting a #
for each line:
- Python Example Code
# This is an example
# of multi-line comments
# in the python programming language
# using multiple short comments
print("Hello, Archangel!")
Another way to implement multi-line comments in Python programming is to use a multiline string.
In the previous tutorial, we showed you how to use multiline strings to assign a large string (paragraph) to a single variable.
To use multiline strings as multi-line comments, do not assign it to a variable:
- Python Example Code
"""
This is an example
of multi-line comments
in the python programming language
using multiline strings.
"""
print("Hello, Archangel!")
You can also use single quotes:
- Python Example Code
'''
This is an example
of multi-line comments
in python programming language
using multiline strings.
'''
print("Hello, Archangel!")
The output of the above programs will be:
- Output
Wrap Off
Comments are fundamental when developing large programs individually or in teams. A rule of thumb is to ensure all comments are clear, precise, and comprehensive. You can write comments in languages other than English.
If you run into errors or unable to complete this tutorial, feel free to contact us anytime, and we will instantly resolve it. You can also request clarification, download this tutorial as a pdf, or report bugs using the buttons below.