IPython (Interactive Python) is an interactive shell that powerfully extends the standard Python interactive shell. Because it facilitates input completion, object information reference, and execution of OS commands, it is widely used in data analysis and script behavior verification.
Here, I will explain the installation method and major features essential for practical work (completion, help, and magic commands).
Installation and Launch
IPython can be installed individually or is included in distributions like Anaconda.
Installation
pip install ipython
Launch and Exit
Enter the following commands in your terminal (or command prompt).
# Launch
ipython
# Exit
exit
# Or press [Ctrl] + [D]
1. Powerful Completion with Tab Key
The biggest feature of IPython is input completion using the Tab key. You can complete variable names, function names, module names, and even file paths.
In [1]: import datetime
# Press Tab after typing 'da' to see candidates like 'datetime' or 'date'
In [2]: da<TAB>
# Press Tab after typing '.' following an object to display a list of methods
In [3]: datetime.date.<TAB>
Display all 14 possibilities? (y or n)
datetime.date.ctime datetime.date.isocalendar ...
2. Checking Object Information with “?” Command
By appending ? or ?? to the end of a variable or function, you can instantly check detailed information about that object. There is no need to write print() functions.
? : Display Document (Docstring)
Displays the type of the variable, function argument definitions, and explanatory text.
In [1]: my_list = [1, 2, 3]
In [2]: my_list?
Type: list
String form: [1, 2, 3]
Length: 3
Docstring:
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list.
The argument must be an iterable if specified.
?? : Display Source Code
Used when you want to display the actual source code of the function definition (only for parts implemented in Python).
In [3]: def my_func(x):
...: return x * 2
...:
In [4]: my_func??
Signature: my_func(x)
Docstring: <no docstring>
Source:
def my_func(x):
return x * 2
File: <ipython-input-3-xxxx>
Type: function
3. Magic Commands and Shell Commands
IPython is equipped with unique convenience commands starting with % (Magic Commands) and the ability to execute OS commands.
Major Magic Commands
| Command | Description |
| %timeit | Measures the execution time of code (runs multiple times to calculate the average).In [1]: %timeit [i**2 for i in range(1000)] |
| %run | Executes an external Python script file.In [2]: %run myscript.py |
| %pwd | Displays the current working directory. |
| %cd | Changes the working directory. |
| %history | Displays input history. |
| %paste | Formats and executes code from the clipboard (prevents indentation errors). |
Execution of OS Commands (!)
By adding ! at the beginning, you can directly execute OS commands such as ls, cd, or pip install.
# Display file list
In [10]: !ls -l
# Install package
In [11]: !pip install requests
4. Referencing Input/Output History (_, In, Out)
You can reuse the previous calculation result or past inputs/outputs as variables.
_(Underscore): Refers to the immediate previous output result (Out)._N(N is a number): Refers to the result ofOut[N].In: Input history is stored as a list.Out: Output history is stored as a dictionary.
In [1]: 10 + 20
Out[1]: 30
In [2]: _ * 2
Out[2]: 60 # The previous result (30) * 2 is calculated
