Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

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
  2. explain what the code does
  3. 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
  2. # Print out a bunch of stuff
  3. # 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.
  2. explain how each part of the code works.
  3. 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.

Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.