Table of Contents » Chapter 1 : Preliminaries : Comments
Comments
Contents
Overview
Comments in Python source code files are used to document your code. The Python interpreter ignores comments, so you can include any text you like within a comment. Comments can be an entire line in the code file, a partial line in the code file, or multiple lines in a code file.
There are three types of comments in Python:
- Entire line: This type of comment begins with a # character in the first position on a line, making the whole line a comment.
- Partial line: This type of comment begins with a # character in a position other than the first position, which makes the rest of the line, from the # position to the end, a comment.
- Multiple lines: This type of comment uses a set of three quote marks ["""] to indicate the beginning and end of a comment block, which is usually on multiple consecutive lines in the code file. Another way to achieve a multiline comment is to begin every line in a group of lines with a # character. See the Keyboard Shortcuts section below for an editor shortcut to make this type of comment easy to create in your editor.
# This is an entire line comment.
print("This is a literal string that prints to the console.")
print("This is another literal string.") # This is a partial line comment.
print("This is a literal string that prints to the console.")
# This is a multiline comment
# in which each comment line
# starts with the # symbol.
print("This is a literal string that prints to the console.")
"""
This is a multiline comment, it can be any number
of lines as long as the comments are between the
two sets of triple-double quotes ...
... including blank lines.
"""
print("This is printing another string.")
Note that a comment can begin with the pound sign (#) symbol. In the example above, we used # for a full line comment or partial line comment. Also, the """ symbol starts and ends a multiline comment block. The Python interpreter ignores everything enclosed in the comment symbols.
Most IDEs and editors have keyboard shortcuts for commenting code. For example, if you press Ctrl / the line your cursor is on will be commented. Pressing Ctrl / again on the same line will uncomment the line. If you have multiple lines selected (highlighted) and press Ctrl / all of the selected lines will be commented and uncommented if you press Ctrl / again. If you use an IDE or editor other than PyCharm, check its documentation to find the equivalent keyboard shortcut.
There is an ongoing debate about how and where to comment source code. Various opinions exist within different groups in IT. At one end of the discussion, some programmers say that your code should be self-documenting. That is, it should be written clearly enough to show others what it does. On the other end of the debate, some programmers say you should heavily comment source code to try to eliminate ambiguity.
Personally, I land in the middle of the two ends of the debate. Either end can be taken to an extreme. Commenting is important to help others who may have to read or maintain your code understand complex constructs, calculations, etc. If you find yourself writing code difficult for others to understand (because it is complex, unorthodox, out of coding standards, etc.), then commenting on it would be very beneficial. On the other hand, if you find yourself writing code like that, it would be wise to reconsider the design and approach to solving the problem.
Problem
Write a Python program that includes the following in the order listed here:
- Write a print() statement that prints the word "Start".
- On the same line as your "Start" print statement, write a partial-line comment that reads "Program starts here"
- Write a single-line comment that contains your first and last name.
- Write a single-line comment that contains the date you wrote this program.
- Write a multi-line comment that contains a brief paragraph (at least 3 sentences) that answers this question: What did you learn in Chapter 1 of this book that you did not know before, or that you found interesting?
- Write a print() statement that prints the words "End".
- On the same line as your "End" print statement, write a partial-line comment that reads "Program ends here"