Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.

Chapter 7: Modules

33 problems · 0 figures

Concepts in Practice 7.1.1 §7.1 source ↗

How many functions are defined in the area module?
  1. a.
    6
  2. b.
    10
  3. c.
    49

Answer

B. 10
The code has ten def statements, each of which defines a function.

Concepts in Practice 7.1.2 §7.1 source ↗

What would be the result of running area.py as a program?
  1. a.
    Functions would be defined and ready to be called.
  2. b.
    Nothing; the module has no statements to be run.
  3. c.
    SyntaxError

Answer

A. Functions would be defined and ready to be called.
Many modules consist of function definitions only. The functions are intended to be called in other modules.

Concepts in Practice 7.1.3 §7.1 source ↗

What is the return value of cube(5)?
  1. a.
    60
  2. b.
    125
  3. c.
    150

Answer

C. 150
area.cube(5) is 6 * 5**2, which is 6 * 25, which is 150 .

Concepts in Practice 7.1.4 §7.1 source ↗

What statement would import a module from a file named secret.py?
  1. a.
    import secret
  2. b.
    import secret.py
  3. c.
    import "secret.py"

Answer

A. import secret
When importing a module from another file, the suffix .py is removed.

Concepts in Practice 7.1.5 §7.1 source ↗

What code would return the area of a circle with a radius of 3 meters?
  1. a.
    circle(3)
  2. b.
    area.circle(3)
  3. c.
    circle.area(3)

Answer

B. area.circle(3)
The circle function must be called using the area variable that refers to the module.

Concepts in Practice 7.1.6 §7.1 source ↗

A programmer would like to write a function that calculates the area of a hexagon. Where should the function be written?
  1. a.
    the main program
  2. b.
    the area module
  3. c.
    the secret module

Answer

B. the area module
Adding the function to the area module makes the function easier to reuse in other programs.

Concepts in Practice 7.2.1 §7.2 source ↗

Which import statement would be needed before running print(sqrt(25))?
  1. a.
    from math import sqrt
  2. b.
    import sqrt from math
  3. c.
    import math

Answer

A. from math import sqrt
This statement imports the sqrt function defined in the math module.

Concepts in Practice 7.2.2 §7.2 source ↗

How many variables are defined by the statement from math import sin, cos, tan?
  1. a.
    0
  2. b.
    3
  3. c.
    4

Answer

B. 3
A variable is defined for each function imported: sin, cos, and tan.

Concepts in Practice 7.2.3 §7.2 source ↗

What error would occur if attempting to call a function that was not imported?
  1. a.
    ImportError
  2. b.
    NameError
  3. c.
    SyntaxError

Answer

B. NameError
A name error occurs when a variable name is not found, which might be due to a missing import statement.

Concepts in Practice 7.2.4 §7.2 source ↗

A program begins with from area import square, circle. What code causes a name collision?
  1. a.
    def area(phone):
      """Gets the area code of a phone number."""
  2. b.
    def circle(x, y, size):
      """Draws a circle centered at (x, y)."""
  3. c.
    def is_square(length, width):
      """Returns True if length and width are equal."""

Answer

B.
def circle(x, y, size):
  """Draws a circle centered at (x, y)."""
This statement replaces the previously imported circle function, even though circle() and area.circle() have different parameters.

Concepts in Practice 7.2.5 §7.2 source ↗

A program begins with import area. What code causes a name collision?
  1. a.
    area = 51
  2. b.
    import cylinder from volume
  3. c.
    def cube(size):
      """Generates a "size X size" rubik's cube."""

Answer

A.
area = 51
Assigning the area variable replaces the previously imported module with the integer 51 . The variable can no longer be used to call functions in the area module.

Concepts in Practice 7.2.6 §7.2 source ↗

Which line will cause an error?
1  def hello():
2    print("Hello!")
3  
4  def hello(name):
5    print("Hello,", name)
6  
7  hello()
8  hello("Chris")
  1. a.
    line 4
  2. b.
    line 7
  3. c.
    line 8

Answer

B. line 7
The most recent definition of hello() requires one argument, so this line would result in an error.

Concepts in Practice 7.3.1 §7.3 source ↗

Which line would cause a side effect when imported?
1  import math
2  
3  print("Defining sphere function")
4  
5  def sphere(radius):
6    """Gets the volume of a sphere."""
7    return 4/3 * math.pi * radius**3
  1. a.
    line 1
  2. b.
    line 3
  3. c.
    line 5

Answer

B. line 3
Whenever this module is imported, Python will output "Defining sphere function" as a side effect.

Concepts in Practice 7.3.2 §7.3 source ↗

The following volume.py module causes a side effect.
import math

def sphere(radius):
  """Gets the volume of a sphere."""
  return 4/3 * math.pi * radius**3

for r in range(10000000):
  volume = sphere(r)
  1. a.
    true
  2. b.
    false

Answer

A. true
Even though nothing is printed, calling the sphere function ten million times causes a delay in the main program.

Concepts in Practice 7.3.3 §7.3 source ↗

The following greeting.py module causes a side effect.
name = input("What is your name? ")
print(f"Nice to meet you, {name}!")
live = input("Where do you live? ")
print(f"{live} is a great place.")
  1. a.
    true
  2. b.
    false

Answer

A. true
All code is at the top level, and no functions are defined. Importing this module would run all code as a side effect.

Concepts in Practice 7.3.4 §7.3 source ↗

What is the output when running the following test.py module?
import math

print(math.__name__)
print(__name__)
  1. a.
    >math
    test
  2. b.
    __main__
    test
  3. c.
    math
    __main__

Answer

C.
math
__main__
The variable __name__ is the module's name, unless the module was run as the main program. In that case, __name__ is "__main__" .

Concepts in Practice 7.3.5 §7.3 source ↗

What is the output when importing the following test.py module?
import math

print(math.__name__)
print(__name__)
  1. a.
    math
    test
  2. b.
    __main__
    test
  3. c.
    math
    __main__

Answer

A.
math
test
Since test.py was imported, the value of __name__ is "test" .

Concepts in Practice 7.3.6 §7.3 source ↗

What line is useful for preventing side effects when importing?
  1. a.
    if __name__ == "main":
  2. b.
    if __name__ == __main__:
  3. c.
    if __name__ == "__main__":

Answer

C. if __name__ == "__main__":
This line is found in many Python modules to check if the module was run as the main program.

Concepts in Practice 7.4.1 §7.4 source ↗

What color is #000080?
  1. a.
    maroon red
  2. b.
    navy blue
  3. c.
    olive green

Answer

B. navy blue
The digits have 0 red and 0 green, so the color must be a shade of blue. Navy is a darker version of standard blue, #0000FF .

Concepts in Practice 7.4.2 §7.4 source ↗

What is 255 in hexadecimal?
  1. a.
    00
  2. b.
    80
  3. c.
    FF

Answer

C. FF
FF in hexadecimal is 255, the maximum amount of red, green, or blue in a color.

Concepts in Practice 7.4.3 §7.4 source ↗

Which color is lighter?
  1. a.
    #FFA500 (orange)
  2. b.
    #008000 (green)

Answer

A. #FFA500 (orange)
The red, green, and blue values for orange are higher than the red, green, and blue values for green.

Concepts in Practice 7.4.4 §7.4 source ↗

What are the components of the color YELLOW?
  1. a.
    red=0, green=255, blue=255
  2. b.
    red=255, green=255, blue=0
  3. c.
    red=255, green=0, blue=255

Answer

B. red=255, green=255, blue=0
The hexadecimal for YELLOW is #FFFF00 , meaning 255 red + 255 green + 0 blue.

Concepts in Practice 7.4.5 §7.4 source ↗

What code would return a darker shade of blue?
  1. a.
    darken(BLUE)
  2. b.
    colors.darken(BLUE)
  3. c.
    colors.darken(colors.BLUE)

Answer

C. colors.darken(colors.BLUE)
Both the darken function and the BLUE variable must be accessed via the colors variable.

Concepts in Practice 7.4.6 §7.4 source ↗

What symbol indicates that a function is not intended to be called by other modules?
  1. a.
    underscore (_)
  2. b.
    number sign (#)
  3. c.
    colon (:)

Answer

A. underscore (_)
Function names that begin with an underscore are intended to be used only within the current module.

Concepts in Practice 7.4.7 §7.4 source ↗

The documentation includes comments from the source code.
  1. a.
    true
  2. b.
    false

Answer

B. false
Only docstrings are included in the documentation. All comments are ignored by the help function.

Concepts in Practice 7.4.8 §7.4 source ↗

In what order are the functions listed in the documentation?
  1. a.
    alphabetical order
  2. b.
    definition order
  3. c.
    random order

Answer

A. alphabetical order
The functions are sorted alphabetically by name to make looking up a function easier.

Concepts in Practice 7.4.9 §7.4 source ↗

Which function defined in colors.py is not included in the documentation?
  1. a.
    _tohex
  2. b.
    tohex
  3. c.
    torgb

Answer

A. _tohex
Functions that begin with an underscore are intended to be used only within a module, not in other modules.

Concepts in Practice 7.5.1 §7.5 source ↗

Use the library reference, module index, and documentation links above to answer the questions.

Which page provides a list of built-in modules sorted by category?
  1. a.
    library reference
  2. b.
    module index
  3. c.
    PEP 2

Answer

A. library reference
This reference describes the standard library, including built-in functions, types, and modules.

Concepts in Practice 7.5.2 §7.5 source ↗

Use the library reference, module index, and documentation links above to answer the questions.

What is the value of calendar.SUNDAY?
  1. a.
    1
  2. b.
    6
  3. c.
    7

Answer

B. 6
As explained in the documentation of the calendar module, MONDAY is 0 and SUNDAY is 6 .

Concepts in Practice 7.5.3 §7.5 source ↗

Use the library reference, module index, and documentation links above to answer the questions.

Which built-in module enables the development of graphical user interfaces?
  1. a.
    tkinter
  2. b.
    turtle
  3. c.
    webbrowser

Answer

A. tkinter
The tkinter module stands for Tk interface. Tk is a standard toolkit for creating windows, text boxes, buttons, and other graphical widgets.

Concepts in Practice 7.5.4 §7.5 source ↗

Use pypi.org and the links in the table above to answer the questions.

Which modules can be used to edit pictures and videos?
  1. a.
    BeautifulSoup and Scrapy
  2. b.
    Bokeh and Matplotlib
  3. c.
    MoviePy and Pillow

Answer

C. MoviePy and Pillow
MoviePy is a module for video editing, and Pillow is a module for image processing.

Concepts in Practice 7.5.5 §7.5 source ↗

Use pypi.org and the links in the table above to answer the questions.

Which third-party module is a replacement for the built-in datetime module?
  1. a.
    arrow
  2. b.
    calendar
  3. c.
    time

Answer

A. arrow
According to the "Features" section of the documentation, Arrow's is a "fully-implemented, drop-in replacement for datetime."

Concepts in Practice 7.5.6 §7.5 source ↗

Use pypi.org and the links in the table above to answer the questions.

Search for the webcolors module on PyPI. What function provided by webcolors looks up the color name for a hex code?
  1. a.
    hex_to_name
  2. b.
    name_to_hex
  3. c.
    normalize_hex

Answer

A. hex_to_name
This function converts a color code to a color name. Ex: hex_to_name("#daa520") returns "goldenrod" .

Loading chapter 8…

Problems and answers are adapted from Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield and Narges Norouzi, OpenStax, and are used under the CC BY-NC-SA 4.0 licence. Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction. Illustrations: Copyright Rice University, OpenStax, under CC BY-NC-SA 4.0 license. This adaptation is released under the same licence, is non-commercial, and is neither affiliated with nor endorsed by OpenStax or Rice University.