Dremendo Tag Line

Comments in Python Programming

Input/Output in Python

In this lesson, we will understand what comments are and how it works in Python programming with the help of some examples.

What is Comments

In Python program Comments are portions of the code ignored by the interpreter which allow the user to make simple notes in the relevant areas of the source code. Comments in Python programming are used to provide information about lines of code. It is widely used for documenting code.

Let's see some examples for more understanding.

Example 1 (Single Line Comments)

# Printing Hello World on the screen.
print('Hello World')  # Now we are going to print Hello World.

Output

Hello World

Here you can see that in line number 1 we have used a single line comments to explain what we are going to do on the next line. You can also write comment after the end of the statement as shown above in line number 2.

Example 2 (Multi-Line Comment)

# In this program we will learn how
# to print Hello World on the screen
print('Hello World')
# print('\nThis is a Python tutorial')

Output

Hello World

Here you can see that from line number 1 to line number 2 we have used multi-line comments to explain what the program is all about. You can also comment on any statement that you don't want to execute in your program but want to keep it for future purpose as shown above in line number 4.

Note: Python does not have any other way to implement multi-line comments. If you want to comment on multiple lines, then you have to put # symbol at the beginning of each line as shown in the example above. But sometimes we can use a multi-line string to implement multi-line comments in our code. Python interpreter will not execute the multi-line string unless and until it is assigned in a variable. Let's see an example for more understanding.

Example (Use of Multi-Line String as Multi-line Comment)

""" In this program we will learn how
to print Hello World on the screen """
print('Hello World')

Here you can see that we have used multi-line string to implement it as a multi-line comment. A multi-line string starts with 3 double quotes """ and ends with 3 double quotes """. In between the opening and closing triple quotes, we will implement our multi-line comments as shown in the example above.

video-poster

Check Your Knowledge


You Scored :

Percentage :