IPython - Getting Started
Getting Started with IPython
Section titled “Getting Started with IPython”This chapter covers the basics of launching and interacting with the IPython environment, highlighting key features that distinguish it from the standard Python interpreter.
Launching IPython
Section titled “Launching IPython”There are several ways to start an IPython session:
- From the Terminal: Simply type
ipythonin your system’s terminal. - Via JupyterLab: Launch JupyterLab (type
jupyter labin the terminal) and open a new ‘Console’ or ‘Notebook’ from the launcher. Notebooks use the IPython kernel by default for Python. - Via Jupyter Notebook: Launch the classic Jupyter Notebook (type
jupyter notebookin the terminal) and create a new Python 3 notebook. - Via Anaconda Navigator: If you installed via Anaconda, you can launch JupyterLab, Notebook, or QtConsole graphically.
For most interactive data science workflows, using JupyterLab or Jupyter Notebook is recommended as they provide a rich web-based interface.
The IPython Prompt
Section titled “The IPython Prompt”Instead of the standard Python >>> prompt, IPython uses numbered prompts:
In [1]:for input (the number increments for each executed input).Out[1]:precedes the output corresponding toIn [1](only shown if the input produces an output).
In [1]: message = "Welcome to IPython"
In [2]: messageOut[2]: 'Welcome to IPython'
In [3]: 2 + 3Out[3]: 5Notice the syntax highlighting, which makes code easier to read.
Tab Completion
Section titled “Tab Completion”IPython offers powerful tab completion. Type the beginning of a variable, function, method, or module name and press Tab.
- If there’s a unique completion, it will be filled in automatically.
- If there are multiple possibilities, a list will pop up for you to choose from.
In [4]: import numpy as np
In [5]: arr = np.array([1, 2, 3])
In [6]: arr.me # Press Tab here# IPython might suggest: arr.mean, arr.median (depending on version/imports)Tab completion also works for file paths within strings.
Object Introspection (?, ??)
Section titled “Object Introspection (?, ??)”To quickly get information about any Python object (variable, function, module, etc.), type the object’s name followed by a question mark ?.
In [7]: np.array?# Output: Displays the docstring for numpy.array, including signature and parameters.Using two question marks ?? provides even more detail, often including the source code of the object if available.
In [8]: def my_function(x): ...: """This is my simple function.""" ...: return x * 2 ...:
In [9]: my_function??# Output: Shows the docstring AND the source code of my_function.Magic Commands (%)
Section titled “Magic Commands (%)”IPython includes special ‘magic’ commands prefixed with % (for line magics) or %% (for cell magics) that provide convenient shortcuts for common tasks. We’ll explore these in detail later.
You can list all available magics with %lsmagic.
In [10]: %lsmagic# Output: Lists available line and cell magics.These fundamental features make IPython a highly efficient environment for interactive Python development and data exploration.