Online Tutorials & Training Materials | STechies.com
Register Login

How to Use Comments in Python?

|| 0

How to Use Comments in Python?
Stechies

In every programming language, comments are an essential part for developers while writing the code. Programmers write a comment to describe the role of their code. In layman language developers use comments to explain the code which help others to understand the code easily.

In python, whenever we write a comment, we put the '#' (hash mark) before every comment. Python compiler does not execute any line in the code, which starts from the '#' hash mark.

In python, there are two types of comments:

  • Single line comment
  • Multiple line comment

Python Single line Comment

To comment out single line from the block of code we use “#” at the beginning of line.

Example:

# Python program to explain Single line comment 

print('Hello Stechies')

Output:

Hello Stechies

In the above example you can see that code block start with “#” hash mark skipped by the python compiler.

Python Multiline Comment

To comment multiple lines we can use ''' (3 times sigle quote or triple single quote) or """ ( 3 times double quote or triple double quote).

We must use ''' ( triple single quotes ) or """ ( triple double quoted ) at the beginning and end of the comments as shown below.

Example:

'''
Python program to explain
multiple line comment
print('Hello')
'''

"""
Python program to explain
multiple line comment
print('Hello')
"""

print('Hello Stechies')

Output:

Hello Stechies

Comment & Uncomment line of codes using Python IDLE ( Python’s Integrated Development and Learning Environment )

Comment Single line of code using Python IDLE

Select the line which you want to comment and then go to

Format >> Comment Out Region or press (Alt + 3)

Comments in Python

Comments in Python-1

Comment Multiple line of code using Python IDLE

Select multiple line which you want to comment and then go to

Format >> Comment Out Region or press (Alt + 3)

Comments in Python-2

Comments in Python-3

Uncomment Single or Multiple line with Python IDLE

Select the line of code you want to uncomment and then go to

Format >> Uncomment Region or press (Alt + 4)

Comments in Python-4

Comments in Python-5