Python: Comments

Thursday 20th July 2017 2:41am

Comments allow software developers to document their code within the code itself. This is especially useful when modifying existing code written by other developers; to be able to read a brief description of what the code is trying to accomplish rather than waste time deciphering source code. There are two ways to define a comment in Python we can use either the # (hash sign) which creates a comment to a single line or for multiple lines we use the ”’ three (single quotes). The computer ignores these comments when the program is executed.

This is an exmple of how to use three single quotes to comment for more detailed descriptions. These types of comments usually appear at the top of the code and describe the developer, date and a brief description of the what the code does:

'''
Written by: Phantom Rasberry Blower
Date: 12/03/2002
This is an exmple of comments using
multiple lines that start with
three single quotes and end with
three single quotes.
'''

The next example shows how to comment single lines using the hash sign:

# These comments are ignored by the computer when the program is executed
pi = 3.14159265359  # Comments can also be placed next to code
# But not before! pi = 3.14159265359 (This won’t work)

During the course of this user guide I have been purposely verbose with my comments; most comments are there to be informative or to explain sections of code that are less intuitive.

FB Like & Share