Python Tutorials – Comments

Comments are very useful to give hints to others who explores the source code later.

Python allows comments in both single line and multi-line.

Single line comments:

This type of comments start with a hash symbol. (#)

a = 10 # This is an integer 
# loop to increment 5
for i in range(1,20,5):
    a = a+ i

Multi-Line Comments:

There are no such multi-line comments in Python.

However, there is a technique we can use it as multi-line comments.

Python compiler will ignore unassigned values while compiling and it will exist in byte code. So, these values will not be executed at runtime.

A String enclosed with 3 single or double quotes is a multi-line string. It can even contain a new line character.

If there is a single quoted or double quoted multiline string, then it will not be included at runtime. It will be useful only for the programmer and not for the end user.

# This is the first line of comment
This is the second line of comment
This is the third line of comment

The above code will print only the string “test”.

However, Good code doesn’t need explanation. Code should be simple and easy to understand! Isn’t it?

Asha Ponraj
Asha Ponraj

Data science and Machine Learning enthusiast | Software Developer | Blog Writter

Articles: 86

Leave a Reply

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