Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Learning objectives

By the end of this section you should be able to

  • Use the help() function to explore a module's contents.
  • Identify portions of code included in the documentation.

Colors on websites

This section introduces an example module for working with HTML colors. HyperText Markup Language (HTML) is used to design websites and graphical applications. Web browsers like Chrome and Safari read HTML and display the corresponding contents. Ex: The HTML code <p style="color: Red">Look out!</p> represents a paragraph with red text.

HTML defines 140 standard color names. Additional colors can be specified using a hexadecimal format: #RRGGBB. The digits RR, GG, and BB represent the red, green, and blue components of the color. Ex: #DC143C is 220 red + 20 green + 60 blue, which is the color Crimson.

Red, green, and blue values range from 0 to 255 (or 00 to FF in hexadecimal). Lower values specify darker colors, and higher values specify lighter colors. Ex: #008000 is the color Green, and #00FF00 is the color Lime.

Checkpoint

HTML color codes

Concepts in Practice

HTML color codes

1.
What color is #000080?
  1. maroon red
  2. navy blue
  3. olive green
2.
What is 255 in hexadecimal?
  1. 00
  2. 80
  3. FF
3.
Which color is lighter?
  1. #FFA500 (orange)
  2. #008000 (green)

Example colors module

A module for working with HTML color codes would be helpful to graphic designers and web developers. The following Python code is in a file named colors.py.

  • Line 1 is the docstring for the module.
  • Lines 3–16 assign variables for frequently used colors.
  • Lines 18–24 define a function to be used within the module.
  • Lines 26–45 define functions to be used in other modules.

Note: The tohex() and torgb() functions use Python features (string formatting and slicing) described later in the book. For now, the documentation and comments are more important than the implementation details.

    """Functions for working with color names and hex/rgb values."""

    # Primary colors
    RED = "#FF0000"
    YELLOW = "#FFFF00"
    BLUE = "#0000FF"

    # Secondary colors
    ORANGE = "#FFA500"
    GREEN = "#008000"
    VIOLET = "#EE82EE"

    # Neutral colors
    BLACK = "#000000"
    GRAY = "#808080"
    WHITE = "#FFFFFF"

    def _tohex(value):
      """Converts an integer to an 8-bit (2-digit) hexadecimal string."""
      if value <= 0:
        return "00"
      if value >= 255:
        return "FF"
      return format(value, "02X")

    def tohex(r, g, b):
      """Formats red, green, and blue integers as a color in hexadecimal."""
      return "#" + _tohex(r) + _tohex(g) + _tohex(b)

    def torgb(color):
      """Converts a color in hexadecimal to red, green, and blue integers."""
      r = int(color[1:3], 16) # First 2 digits
      g = int(color[3:5], 16) # Middle 2 digits
      b = int(color[5:7], 16) # Last 2 digits
      return r, g, b

    def lighten(color):
      """Increases the red, green, and blue values of a color by 32 each."""
      r, g, b = torgb(color)
      return tohex(r+32, g+32, b+32)

    def darken(color):
      """Decreases the red, green, and blue values of a color by 32 each."""
      r, g, b = torgb(color)
      return tohex(r-32, g-32, b-32)

Concepts in Practice

The colors module

4.
What are the components of the color YELLOW?
  1. red=0, green=255, blue=255
  2. red=255, green=255, blue=0
  3. red=255, green=0, blue=255
5.
What code would return a darker shade of blue?
  1. darken(BLUE)
  2. colors.darken(BLUE)
  3. colors.darken(colors.BLUE)
6.
What symbol indicates that a function is not intended to be called by other modules?
  1. underscore (_)
  2. number sign (#)
  3. colon (:)

Module documentation

The built-in help() function provides a summary of a module's functions and data. Calling help(module_name) in a shell is a convenient way to learn about a module.

Example 7.2

Output of help(colors) in a shell

The documentation below is automatically generated from the docstrings in colors.py.

help(colors)

    Help on module colors:

    NAME
      colors - Functions for working with color names and hex/rgb values.

    FUNCTIONS
      darken(color)
        Decreases the red, green, and blue values of a color by 32 each.

      lighten(color)
        Increases the red, green, and blue values of a color by 32 each.

      tohex(r, g, b)
        Formats red, green, and blue integers as a color in hexadecimal.

      torgb(color)
        Converts a color in hexadecimal to red, green, and blue integers.

    DATA
      BLACK = '#000000'
      BLUE = '#0000FF'
      GRAY = '#808080'
      GREEN = '#008000'
      ORANGE = '#FFA500'
      RED = '#FF0000'
      VIOLET = '#EE82EE'
      WHITE = '#FFFFFF'
      YELLOW = '#FFFF00'

    FILE
      /home/student/Desktop/colors.py
    >>> help(colors)

    Help on module colors:

    NAME
      colors - Functions for working with color names and hex/rgb values.

    FUNCTIONS
      darken(color)
        Decreases the red, green, and blue values of a color by 32 each.

      lighten(color)
        Increases the red, green, and blue values of a color by 32 each.

      tohex(r, g, b)
        Formats red, green, and blue integers as a color in hexadecimal.

      torgb(color)
        Converts a color in hexadecimal to red, green, and blue integers.

    DATA
      BLACK = '#000000'
      BLUE = '#0000FF'
      GRAY = '#808080'
      GREEN = '#008000'
      ORANGE = '#FFA500'
      RED = '#FF0000'
      VIOLET = '#EE82EE'
      WHITE = '#FFFFFF'
      YELLOW = '#FFFF00'

    FILE
      /home/student/Desktop/colors.py

Concepts in Practice

The help() function

7.
The documentation includes comments from the source code.
  1. true
  2. false
8.
In what order are the functions listed in the documentation?
  1. alphabetical order
  2. definition order
  3. random order
9.
Which function defined in colors.py is not included in the documentation?
  1. _tohex
  2. tohex
  3. torgb

Try It

Help on modules

The random and statistics modules are useful for running scientific experiments. You can become familiar with these two modules by skimming their documentation.

Open a Python shell on your computer, or use the one at python.org/shell. Type the following lines, one at a time, into the shell.

  • import random
  • help(random)
  • import statistics
  • help(statistics)

Many shell environments, including the one on python.org, display the output of help() one page at a time. Use the navigation keys on the keyboard (up/down arrows, page up/down, home/end) to read the documentation. When you are finished reading, press the Q key ("quit") to return to the Python shell.

Try It

Help on functions

The help() function can be called on specific functions in a module. Open a Python shell on your computer, or use the one at python.org/shell. Type the following lines, one at a time, into the shell.

  • import random
  • help(random.randint)
  • help(random.choice)
  • import statistics
  • help(statistics.median)
  • help(statistics.mode)

Remember to use the navigation keys on the keyboard, and press the Q key ("quit") to return to the Python shell.

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.