Getting Started
2 snippetsIPython interactive shell basics
Magic Commands
# Line magic (single %)
%pwd # Print working directory
%ls # List files
%cd /path/to/directory
# Cell magic (double %%)
%%time
sum(range(1000000))Shell Commands
# Run shell commands with !
!ls -la
!pwd
!python --version
# Capture output
files = !ls
print(files)Help & Inspection
3 snippetsExploring objects and documentation
Get Help
# Question mark for help
len?
str.split?
# Double question mark for source
len??
# dir() for attributes
dir(str)Tab Completion
# Type and press Tab
import numpy as np
np.ar<TAB> # Shows array, arange, etc.
# Works with file paths
!cat ~/Doc<TAB>Wildcard Search
# Find matching names
*Warning*?
str.*find*?Common Magic Commands
4 snippetsUseful IPython magics
Timing Code
# Time single line
%timeit sum(range(1000))
# Time cell
%%timeit
total = 0
for i in range(1000):
total += iRun Scripts
# Run Python file
%run script.py
# Run with profiler
%run -p script.py
# Run and debug on exception
%run -d script.pyLoad & Save
# Load code from file
%load script.py
# Save cell to file
%%writefile output.py
def hello():
print("Hello, World!")History
# Show command history
%history
# Show last n commands
%history -n 10
# Save history to file
%save mycode.py 1-10Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Variable Management
3 snippetsWorking with variables
List Variables
# List all variables
%who
# List with types
%whos
# List specific types
%who str
%who intDelete Variables
# Delete specific variable
del my_var
# Delete all variables
%reset
# Delete without confirmation
%reset -fStore Variables
# Store to database
%store myvar
# Restore from database
%store -r myvar
# List stored
%storeDebugging
2 snippetsIPython debugger (pdb)
Start Debugger
# Automatic debug on exception
%pdb on
# Manual breakpoint
import pdb; pdb.set_trace()
# Or use IPython debugger
from IPython.core.debugger import set_trace
set_trace()Debug Commands
# In debugger:
# n - next line
# s - step into
# c - continue
# l - list code
# p var - print variable
# q - quit
# h - helpSystem Interaction
3 snippetsOS and environment interaction
Environment Variables
# Set environment variable
%env MY_VAR=value
# Get environment variable
%env MY_VAR
# List all env vars
%envChange Directory
# Change directory
%cd /path/to/dir
# Go to home
%cd ~
# Go back
%cd -Bookmark Directories
# Create bookmark
%bookmark myproject /path/to/project
# Use bookmark
%cd myproject
# List bookmarks
%bookmark -lMatplotlib Integration
2 snippetsPlotting in IPython
Enable Plotting
# For Jupyter notebooks
%matplotlib inline
# For interactive plots
%matplotlib notebook
# For Qt backend
%matplotlib qtQuick Plot
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()Advanced Features
4 snippetsPower user features
Macro Recording
# Start recording
%macro mymacro 1-5
# Run macro
mymacro
# Edit macro
%edit mymacroPaste Code
# Paste code and execute
%paste
# Paste without executing
%cpasteOutput Caching
# Access previous output
_ # Last output
__ # Second to last
___ # Third to last
# Access by line number
Out[5] # Output from cell 5
_5 # Same as aboveParallel Computing
# Start IPython cluster
!ipcluster start -n 4
# Use parallel magic
from ipyparallel import Client
rc = Client()
view = rc[:]