Skip to content

Python Documentation

In this activity, we introduce an important practical programming skill: how to look up Python command instructions and documentation. Python programmers do not memorise every command, function, method or argument. Instead, they learn how to locate reliable documentation, read examples, and adapt them to their own work.

Documentation explains what a command or function does, what inputs it accepts, what values it returns, and how it should be used correctly. This is especially important in data science, where most work relies on external libraries such as NumPy, Pandas, Matplotlib and Scikit-learn.

Built-in Help

Python includes a built-in help system. This can be used to inspect functions, objects and data types directly from the programming environment.

# Look up information about common Python objects

help(print)       # Documentation for the print function
help(list)        # Documentation for the list object
help(dict)        # Documentation for dictionaries

# Try changing the command inside help()
# For example: help(str), help(float), help(sum)

Documentation in Jupyter Notebook

Jupyter Notebook provides quick ways to inspect commands while working. Adding a question mark after a function or object can display documentation inside the notebook interface.

# These commands are especially useful in Jupyter Notebook

print?            # Inspect the print function
sum?              # Inspect the sum function

numbers = [1, 2, 3, 4, 5]
numbers?          # Inspect the list object stored in numbers

# You can also type numbers. and press Tab
# to view available methods such as append, count and sort.

Reading Function Signatures

A function signature shows the name of a function, the arguments it accepts, and any default values. Reading signatures carefully helps avoid many common programming errors.

# The sorted function returns a new sorted list

prices = [104.5, 99.2, 101.8, 97.6]

# Look up the documentation before using the function
help(sorted)

sorted_prices = sorted(prices)

print("Original prices:", prices)
print("Sorted prices:", sorted_prices)

Searching Online Documentation

When using external libraries, the official documentation is usually the best starting point. Useful search phrases combine the library name with the task you are trying to complete.

# Example search terms for documentation

# python list append example
# pandas read csv documentation
# matplotlib line plot example
# numpy calculate mean
# python dictionary keys method

# Good programming practice:
# 1. Search the documentation
# 2. Read the function arguments
# 3. Test a small example
# 4. Adapt the example to your own problem

In the next activity, we introduce loops, which allow Python commands to be repeated automatically.