Python

Python Programming Comments – Detailed FAQ

Although comments don’t alter a program’s output, they are nonetheless crucial to all programming, not just Python. By outlining what we have done in code in plain English, comments help make code more readable. We will learn about comments in Python and their many types in this article.

Python remarks

Before moving on, let’s look at a Python comment example. First, let’s examine the necessity of comments in Python and other programming languages.

A comment is a piece of text used to explain what has been done to a program or what is being done in a block of code; it has no bearing on how the code will function. This is very useful when you are reviewing someone else’s code to repair bugs or change the logic; by reading a comment, you may comprehend the goal of the code much more quickly than by simply reading the actual code.

Let’s examine how a comment appears in Python.

# This is just a text, it won't be executed.

print("Python comment example")

Output:
Python comments example

Types of Python Comments

Python comments can be of two different sorts.
1. A single-line remark
2. Comment with several lines

One-line remarks

In Python, the comment is started with the special character #. To further grasp the usage, let’s look at a few samples.

# This is just a comment. Anything written here is ignored by Python

Triple single quotes are used at the start and end of a multi-line comment in Python, as demonstrated in the example below.

'''
This is a 
multi-line
comment
'''

Example of Python Comments

These three forms of comments are present in this Python program. There are three types of comments: single-line comments, multi-line comments, and comments that begin on the same line as the code.

'''
We are writing a simple program here
First print statement.
This is a multiple line comment.
'''
print("Hello Guys")

# Second print statement
print("How are You all?")

print("Welcome to BeginnersBook") # Third print statement

Output:
Python single line and multiple line comments

# of characters within quotes

When the character # appears within quotation marks, it is not regarded as a comment. For instance:

print("#this is not a comment")

Output:

#this is not a comment

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button