General purpose functions

The following are general purpose functions for managing variables, workspace, and output in IPython or Python interactive sessions. These are analogous to MATLAB’s general functions.

General functions

help

Purpose:

Display documentation for a specific function or object.

Syntax:
help(function_name)
function_name?
function_name??
Description:

help(function_name) displays the docstring/documentation for the specified function. function_name? shows a summary in IPython. function_name?? shows the full docstring and source if available.

Example:

Typing

help(len)
len?
len??

yields documentation for the len function.

Note:

In IPython, ? and ?? are convenient shortcuts.

type

Purpose:

Show the type of an object.

Syntax:
type(obj)
Description:

type(obj) returns the type of the object.

Note:

To display the source code of a function in IPython, use function_name??.

what

Purpose:

List variables in the current namespace.

Syntax:
%who
%whos
Description:

%who lists variable names in the interactive namespace. %whos gives a detailed list with type and info.

Note:

These are IPython magic commands.

Purpose:

Line continuation.

Syntax:
...
Description:

In Python, ... is used as an Ellipsis object or for line continuation in some contexts (e.g., multi-line lambdas or function definitions). For line continuation, use the backslash \ or parentheses.

Note:

In IPython, multi-line statements are handled automatically.

#

Purpose:

Write a comment line.

Syntax:
# arbitrary text
Description:

Any text after # is a comment.

Note:

This is standard Python syntax.

Variables and workspace

clear

Purpose:

Remove variables from workspace.

Syntax:
del var1, var2
%reset
Description:

del var1 deletes a variable. %reset (IPython magic) clears all variables (asks for confirmation).

Note:

Use with caution; %reset cannot be undone.

disp

Purpose:

Display a variable.

Syntax:
print(A)
display(A)  # in Jupyter/IPython
Description:

print(A) prints the value of A. display(A) (from IPython.display) can be used for rich display in Jupyter.

load

Purpose:

Load variables from disk.

Syntax:
import numpy as np
A = np.load('filename.npy')
data = np.loadtxt('filename.txt')
Description:

Use np.load for binary NumPy files, np.loadtxt or pandas.read_csv for text files.

Note:

There is no direct equivalent to MATLAB’s load for .mat files, but scipy.io.loadmat can be used for MATLAB files.

save

Purpose:

Save variables to disk.

Syntax:
np.save('filename.npy', A)
np.savetxt('filename.txt', A)
Description:

Use np.save for binary, np.savetxt for text files. For multiple variables, consider using np.savez or pickle.

who, whos

Purpose:

List variables in the workspace.

Syntax:
%who
%whos
Description:

%who lists variable names. %whos gives detailed info.

Note:

These are IPython magic commands.

Files and command window

diary

Purpose:

Save session output to a file.

Syntax:
%logstart filename
%logstop
Description:

%logstart filename begins logging input/output to a file. %logstop stops logging.

Note:

These are IPython magic commands.

echo

Purpose:

Not directly applicable in Python. For script debugging, use print statements or logging.

Note:

No direct equivalent. Use print or the logging module.

format

Purpose:

Control output display format.

Syntax:
# For NumPy arrays:
np.set_printoptions(precision=5)
np.set_printoptions(precision=15, suppress=True)
Description:

Use np.set_printoptions to control NumPy array display precision.

Note:

For pandas DataFrames, use pd.set_option.

quit

Purpose:

Terminate session.

Syntax:
exit()
quit()
Description:

exit() or quit() exits the Python interpreter or IPython session.

Note:

In Jupyter, use exit() in a cell.