Skip to content
Laviyx Learn

Try

Python Lab · Experiment 1

Explore python.org and the help() utility

PythonWeek 1JNTUH R22 · CS206ES

Aim

To visit the Python website, start the Python interpreter and use the online help utility help(), and to write a short Python program that prints the Python version and shows the help text of a built-in function and of a function we write ourselves.

Algorithm

  1. Open a web browser and go to https://python.org. Look at the Documentation, Downloads and About sections, and try the search box.
  2. Open a terminal (Command Prompt on Windows) and type python (or python3 on Linux) to start the interpreter. The >>> prompt appears.
  3. Type help() and press Enter. The prompt changes to help>. Type a topic such as len, print or keywords to read about it, and type quit to go back to >>>.
  4. Now write a program: import the sys module and print the major version number of Python.
  5. Print len.__doc__, the short description stored inside the built-in function len.
  6. Write a small function with a docstring and call help() on it, so the help text is printed straight away.

Program

Python
import sys


def area_of_circle(radius):
    """Return the area of a circle with the given radius."""
    return 3.14159 * radius * radius


# Which version of Python is running this program?
print("Python major version:", sys.version_info.major)
print()

# Every function carries its own description, called a docstring
print("Docstring of len:", len.__doc__)
print("len('python') =", len("python"))
print()

# help() with an argument prints the help text and returns at once
help(area_of_circle)
print("area_of_circle(2) =", area_of_circle(2))

Output

Output
Python major version: 3

Docstring of len: Return the number of items in a container.
len('python') = 6

Help on function area_of_circle in module __main__:

area_of_circle(radius)
    Return the area of a circle with the given radius.

area_of_circle(2) = 12.56636

Recorded by running this exact program.

How it works

This week's first task is mostly hands-on: browsing python.org and using help() at the >>> prompt cannot be recorded as program output, so they are described in the algorithm. The program above shows the same help system working from a script.

help() has two forms. Typed with no argument at the >>> prompt, it starts an interactive help session with a help> prompt, and you leave it by typing quit. Called with an argument, such as help(len) or help(area_of_circle), it prints the help for that one object and returns at once. That second form is what the program uses.

The help text of a function comes from its docstring: the string written on the first line inside the function body. Writing a docstring for your own functions makes help() work for them too.

On python.org, the Documentation section holds the official tutorial and the Library Reference. The tutorial is a good place to start after this lab.

Common mistakes

  • Typing help() inside a program file and expecting the help> prompt. With no argument it is meant for the interactive interpreter. In a script, pass the object you want, such as help(len).

  • Forgetting how to leave the help utility. At the help> prompt type quit (or press Enter on an empty line) to return to >>>. To leave the interpreter itself, type exit().

  • Typing python on Windows and getting the Microsoft Store page instead of Python. Install Python from python.org and tick 'Add python.exe to PATH' during setup.

  • Putting the docstring after another statement inside the function. Only a string written as the very first statement becomes the docstring.

Viva questions

Try to answer each one aloud before you open it.

  • What does help() do?

    help() is Python's built-in help system. With no argument it starts an interactive help session at the help> prompt. With an argument, like help(len), it prints the documentation of that object.

  • What is a docstring?

    A docstring is a string written as the first statement of a function, class or module. Python stores it in the __doc__ attribute, and help() displays it.

  • What is the difference between the interactive mode and the script mode of Python?

    In interactive mode you type one statement at the >>> prompt and the interpreter runs it at once and shows the result. In script mode you save the statements in a .py file and run the whole file with python file.py.

  • How do you find out which version of Python is installed?

    Run python --version in a terminal, or inside a program use sys.version or sys.version_info from the sys module.

  • How do you leave the interactive help utility and the interpreter?

    Type quit at the help> prompt to return to >>>. Type exit() at >>> to leave the interpreter.

Learn the concepts

All Python Programming Lab experiments →