Menu Close
Python Comments

Python Comments

In this tutorial, you will learn What is Python comments as well as you will learn How can you use Python comments in your program to make your code more readable.

InIn the previous Python tutorial we have seen all about Python Literals using appropriate examples. Here we will see all about comments in Python.


Comments are a description of the Python program that helps programmers to understand the code in a better way. if you use comments in your Python program, Then comment completely ignored by the Python interpreter.

Advantage of using Python comments :

To use python comments in the program, we can make our code more readable or understandable.
using comments you can describe your block of code. If you use comments in your python program, Then it helps other programmers to understand your code.

Before going through this tutorial, let’s understand what is comments in Python.

What is Python comments ?

In the Python program, comments are the most important thing because comments are the programmer’s tools to make more readable code.
With the help of comments, the programmer can explain their code. In code there are two types of comments that are available first is a single-line comment and the second is multiple line comment.
When you use Python comments in your code, Then during run time comments completely ignored by the python interpreter.

One most important fact of comments is, When you come back after long time on your code, Then you can easily understand your code.

Let’s understand the types of python comments and how can we use comments in our python code?

Python Single Line Comment:

Single line comment refer to the only single line to describe your code.
To use the single line comment in python code you need to use hash (#) to beginning of
your line.

You can also use multiple line comments using the hash (#). If you want to use multiple line comment
using hash (#) then you can use the hash (#) sign at the beginning of every line in your code.

Example: Use Python single line comment

#This line prints small message.
x = 'Programming'
y = 'Funda'
print(f'{x} {y} is the best place to learn to code.')

Output

Programming Funda is the best place to learn to code.

Here is the comment:

#This line prints a small message.

The above line completely ignored by the Python interpreter.
Everything that comes after the hash symbol (#) completely ignored by the Python interpreter.

You can write above Program like below.

x = 'Programming'
y = 'Funda'
print(f'{x} {y} is the best place to learn to code.') #This line prints a small message.

Python Multi Line Comment:

Python officially does not provide any separate method to make multiple line comment. But still, you can use multiple line comments in your program by adding a hash symbol at first of each line.

Example: Use Python multi line comment

#Create a variable x
#Create a variable y
#Print small message using variables x and y
x = 'Programming'
y = 'Funda'
print(f'{x} {y} is the best place to learn to code.')

Output

Programming Funda is the best place to learn to code.

In above program, Each line treated as single line comment and all lines will completely ignore by Python interpreter.

String Literal to single-line Python comment.

Python does not provide any other way to write a single line and multi-line comments in your code but still, you can use a single line comment using a single quote because string literally ignore by the Python interpreter.

Example

'Additin of two variables'
x = 12
y = 12
print(x + y)

Output

24

The quotation character can either be ‘ or “.

String Liter for Multi-line Python comments:

To write Python multi line comments you can use triple quotes (”’ or “””).

Example

'Additin 
of two 
variables
'
x = 12
y = 12

Output

24

Python Docstring:

Python Docstring is also called Python documentation string. It comes right after the Python function, class, and method definition to describe the block of code.
Python docstring written within the triple quotation ( ”’ or “”” ).
You can click here to learn More details about Python docstring.

Example

def Addition(x, y):
    ''' This function add the two numbers '''
    print(x + y)
	
Addition(12, 34)

Output

46

How to write best Python comments?

  • Use comments to describe what a function does.
  • Remove redundant comments in your program.
  • Try to write your Python comments in short and concise as possible.
  • Write only major point in Python comments.

Conclusion:

In this tutorial, You have learned about Python comments, Comments Types and Comments usages.You can use Python comments to make your code to make more readable for you and others.
I hope this tutorial we have helped you.If you like this Python comments tutorial please comment and share with your friends who want to learn Python programming from scratch to advanced.

Previous Article
Next Article

Was this post helpful?

Related Posts

Leave a Reply