Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

1.7: Comments

( \newcommand{\kernel}{\mathrm{null}\,}\)

Learning Objectives

By the end of this section you should be able to

  • Write concise, meaningful comments that explain intended functionality of the code.
  • Write a docstring (more verbose comment) that describes the program functionality.

The hash character

Comments are short phrases that explain what the code is doing. Ex: Lines 1, 8, and 10 in the following program contain comments. Each comment begins with a hash character (#). All text from the hash character to the end of the line is ignored when running the program. In contrast, hash characters inside of strings are treated as regular text. Ex: The string "Item #1: " does not contain a comment.

When writing comments:

  • The # character should be followed by a single space. Ex: # End of menu is easier to read than #End of menu.
  • Comments should explain the purpose of the code, not just repeat the code itself. Ex: # Get the user's preferences is more descriptive than # Input item1 and item2.
Example 1.2

Program with three comments

1
# Display the menu options
2
print("Lunch Menu")
3
print("----------")
4
print("Burrito")
5
print("Enchilada")
6
print("Taco")
7
print("Salad")
8
print() # End of menu
9
 
10
# Get the user's preferences
11
item1 = input("Item #1: ")
12
item2 = input("Item #2: ")
Concepts in Practice: Simple comments
1.
The main purpose of writing comments is to _____.
  1. avoid writing syntax errors
  • explain what the code does
  • make the code run faster
  • 2.
    Which symbol is used for comments in Python?
    1. #
    2. /*
    3. //
    3.
    Which comment is formatted correctly?
    1. 0 spaces:
      #Get the user input
    2. 1 space:
      # Get the user input
    3. 2 spaces:
      # Get the user input

    Code quality

    The example program above had two parts: (1) display the menu options, and (2) get the user's preferences. Together, the blank lines and comments show the overall structure of the program.

    Programmers spend more time reading code than writing code. Therefore, making code easier for others to read and understand is important. Two ways to improve code quality include:

    • Separate each part (lines that have a similar purpose) with a blank line.
    • Write a comment before each part. Not every line needs a comment.
    Checkpoint: Comments in a program
    Concepts in Practice: Code quality
    4.
    Which comment is most useful for the following code?
    print("You said:", adj1 + " " + noun1)
    1. # Append adj1 and noun1
  • # Print out a bunch of stuff
  • # Show the resulting phrase
  • 5.
    Where should a blank line be inserted?
    1
    name = input("Whose birthday is today? ")
    2
    print("Happy birthday to", name)
    3
    print("Everyone cheer for", name)
    1. After line 1
    2. After line 2
    3. After line 3
    6.
    To temporarily prevent a line from being run, one might . . .
    1. introduce a syntax error in the line.
    2. remove the line from the program.
    3. insert a # at the beginning of the line.

    Documentation

    Python programs may optionally begin with a string known as a docstring. A docstring is documentation written for others who will use the program but not necessarily read the source code. Most of the official documentation at docs.python.org is generated from docstrings.

    Documentation can be long, so docstrings are generally written as multi-line strings ("""). Common elements of a docstring include a one-line summary, a blank line, and a longer description.

    Checkpoint: Vacations docstring
    Concepts in Practice: Documentation
    7.
    The main purpose of writing docstrings is to . . .
    1. summarize the program's purpose or usage.
  • explain how each part of the code works.
  • maintain a list of ideas for new features.
  • 8.
    Which of the following is NOT a docstring?
    1. """Vacations Madlib."""
    2. """Vacations Madlib.
      This program asks the user for two adjectives
      and two nouns, which are then used to print
      a funny story about a vacation.
      """
    3. # Vacations Madlib.
      #
      # This program asks the user for two adjectives
      # and two nouns, which are then used to print
      # a funny story about a vacation.
    9.
    Which docstring is most useful for this program?
    1. """Vacations Madlib."""
    2. """Vacations Madlib. 
      
      This program asks the user for two adjectives
      and two nouns, which are then used to print
      a funny story about a vacation.
      """
    3. """Vacations Madlib.
      
      This program asks the user for two adjectives
      and two nouns, which are then used to print
      a funny story about a vacation. The code uses
      four variables to store the user input: two for
      the adjectives, and two for the nouns. The
      output is displayed on seven lines, beginning
      with a blank line after the input.
      """
    Try It: Whose birthday

    Add two comments to the following program: one for the input, and one for the output. Separate the input and output with a blank line. Then, compare your comments with the sample solution, and ask yourself the following questions:

    • Are your comments longer or shorter? Why?
    • Is the formatting of your comments correct?
    Try It: Gravity calculation

    Write a docstring for the following program. The first line of the docstring should explain, in one short sentence, what the program is. The second line of the docstring should be blank. The third and subsequent lines should include a longer explanation of what the program does. At the end of the docstring, add a line that says "Author: " followed by your name.


    This page titled 1.7: Comments is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

    Support Center

    How can we help?