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
- Open a web browser and go to https://python.org. Look at the Documentation, Downloads and About sections, and try the search box.
- Open a terminal (Command Prompt on Windows) and type
python(orpython3on Linux) to start the interpreter. The>>>prompt appears. - Type
help()and press Enter. The prompt changes tohelp>. Type a topic such aslen,printorkeywordsto read about it, and typequitto go back to>>>. - Now write a program: import the
sysmodule and print the major version number of Python. - Print
len.__doc__, the short description stored inside the built-in functionlen. - Write a small function with a docstring and call
help()on it, so the help text is printed straight away.
Program
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
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.56636Recorded 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 thehelp>prompt. With no argument it is meant for the interactive interpreter. In a script, pass the object you want, such ashelp(len).Forgetting how to leave the help utility. At the
help>prompt typequit(or press Enter on an empty line) to return to>>>. To leave the interpreter itself, typeexit().Typing
pythonon 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 thehelp>prompt. With an argument, likehelp(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, andhelp()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.pyfile and run the whole file withpython file.py.How do you find out which version of Python is installed?
Run
python --versionin a terminal, or inside a program usesys.versionorsys.version_infofrom thesysmodule.How do you leave the interactive help utility and the interpreter?
Type
quitat thehelp>prompt to return to>>>. Typeexit()at>>>to leave the interpreter.