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 += i Run Scripts
# Run Python file
%run script.py
# Run with profiler
%run -p script.py
# Run and debug on exception
%run -d script.py Load & 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-10 Tired 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 int Delete Variables
# Delete specific variable
del my_var
# Delete all variables
%reset
# Delete without confirmation
%reset -f Store Variables
# Store to database
%store myvar
# Restore from database
%store -r myvar
# List stored
%store Debugging
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 - help System 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
%env Change 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 -l Matplotlib Integration
2 snippetsPlotting in IPython
Enable Plotting
# For Jupyter notebooks
%matplotlib inline
# For interactive plots
%matplotlib notebook
# For Qt backend
%matplotlib qt Quick 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 mymacro Paste Code
# Paste code and execute
%paste
# Paste without executing
%cpaste Output 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 above Parallel Computing
# Start IPython cluster
!ipcluster start -n 4
# Use parallel magic
from ipyparallel import Client
rc = Client()
view = rc[:]