Python Comments

In programming languages, comments are inline non executable statements that are made to explain the functionality of methods, classes, variables or to inform other developers who will interact with the code in future on areas they should be careful while handling code changes.

It is a good practice to comment our code because it helps in reducing the cost of maintaining the program in future as the developers in future will not have to take a lot of time understanding the program.

In python comments are preceded by the # symbol. Any code/text after the # will be ignored.

Single Line comments

# is at start of the comment sentence

Example

When the above program is run, the output is as below

Multi line comments

To write comments that span multiple lines in Python,

we place the # symbol at beginning of every line

Example

#multi line comments

#name hold the person's name

#dob is the date of birth

#age in years

 

When the above program is run, the output is as below

Multi line comments using String literals

Enclose your multi line comment string within triple double quotes (""")or triple single quotes(''').Python ignores such un assigned string literals and in essence they serve as multi line comments.

Example

"""

multi line comments  - 2

name hold the person's name

dob is the date of birth

age in years

"""

When the above program is run, output is as below

About the Author - John Kyalo Mbindyo(Bsc Computer Science) is a Senior Application Developer currently working at NCBA Bank Group,Nairobi- Kenya.He is passionate about making programming tutorials and sharing his knowledge with other software engineers across the globe. You can learn more about him and follow him on  Github.