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
lenfunction.- 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:
%wholists variable names in the interactive namespace.%whosgives 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 var1deletes a variable.%reset(IPython magic) clears all variables (asks for confirmation).- Note:
Use with caution;
%resetcannot be undone.
disp¶
- Purpose:
Display a variable.
- Syntax:
print(A) display(A) # in Jupyter/IPython- Description:
print(A)prints the value ofA.display(A)(fromIPython.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.loadfor binary NumPy files,np.loadtxtorpandas.read_csvfor text files.- Note:
There is no direct equivalent to MATLAB’s
loadfor .mat files, butscipy.io.loadmatcan 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.savefor binary,np.savetxtfor text files. For multiple variables, consider usingnp.savezorpickle.
who, whos¶
- Purpose:
List variables in the workspace.
- Syntax:
%who %whos- Description:
%wholists variable names.%whosgives 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 filenamebegins logging input/output to a file.%logstopstops 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
printor theloggingmodule.
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_printoptionsto control NumPy array display precision.- Note:
For pandas DataFrames, use
pd.set_option.
quit¶
- Purpose:
Terminate session.
- Syntax:
exit() quit()- Description:
exit()orquit()exits the Python interpreter or IPython session.- Note:
In Jupyter, use
exit()in a cell.