Oct. 10, 2012

How to use comments in Python

Comments

Comments in Python are used to explain what the code does. 

Python Comments

Python has two ways to annotate Python code. 

One is by using comments to indicate what some part of the code does. 

Single-line comments begin with the hash character ("#") and are terminated by
the end of line. 

Python is ignoring all text that comes after the # to the end of the line, 
they are not part of the command. 

Comments spanning more than one line are achieved by inserting a multi-line string
(with """ as the delimiter one each end) that is not used in assignment or 
otherwise evaluated, but sits in between other statements.

They are meant as documentation for anyone reading the code. 

Example

Let's show this by using an example
#this is a comment in Python

print "Hello World" #This is also a comment in Python

""" This is an example of a multiline
comment that spans multiple lines
...
"""
Remember to comment often to explain what the codes does

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.

 



Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.