Subscribe Contact

Home  »  Chapter 2 : Programming Basics
Comments

Overview

Comments in Python source code files are used to document your code. The Python interpreter ignores comments in your code. Comments can be an entire line in the code file, a partial line in the code file, or multiple lines in a code file.

Concept: Comments

Comments in programming are annotations in the source code that are not executed by the compiler or interpreter. They are used to provide explanations or clarifications about the code, making it easier for others (and the programmer themselves) to understand the purpose, functionality, and logic of the code at a later time. Comments are crucial for maintaining and updating code, especially in collaborative projects. They can also be used to temporarily disable code during debugging or development without actually deleting the code. Different programming languages have different syntax for comments. In Python, we use the symbols #, ''' ''', and """ """ to indicate commented code. Comments are an essential tool for effective communication within the code, enhancing its readability and maintainability.


Here's an example of code with all three types of comments:

# 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 multi-line comment
# in which each comment line
# starts with the # symbol.
print("This is a literal string that prints to the console.")
"""
This is a multi-line 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 multi-line comment block. The Python interpreter ignores everything enclosed in the comment symbols.

Keyboard Shortcuts

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.

Comments in Production Systems

There is an ongoing debate about how and where to comment source code. Various opinions exist within different groups in IT. On one end of the debate, some programmers say that your code should be self-documenting. That is, it should be written clearly enough to be obvious to 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.

I land in the middle of the two ends of the debate. Either end can be extreme. I think 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 that would be 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.


«  Previous : Text (a.k.a. Strings)
Next : Data & Data Types  »




© 2023 John Gordon
Cascade Street Publishing, LLC