Here are some of the keyboard shortcuts and text snippets I’ve shared with others during Pair Programming sessions that have been well received. They’ve saved me countless hours programming and my hope is you’ll be able to start using some of these techniques to become a more efficient Python programmer.
Taking a few minutes to learn certain Jupyter Notebook keyboard shortcuts has helped me be a more efficient Python developer. Below are the keyboard shortcuts I’ve found most useful.
NOTE these keyboard shortcuts are for Jupyter version 4.1.0 and Mac OSX. For most shortcuts below, you can replace
ctrl for Windows or Linux. Or, you can use the
H keyboard shortcut in Windows or Linux to confirm the appropriate keyboard shortcuts for those operating systems.
Practice Jupyter Notebook
I created this Jupyter Notebook on my Github repo that you can download and use to practice these keyboard shortcuts.
Command mode vs. Edit mode
But first…something key to be aware of: Jupyter Notebooks have two different keyboard input modes:
- Command mode - binds the keyboard to notebook level actions. Indicated by a grey cell border with a blue left margin.
- Edit mode - when you’re typing in a cell. Indicated by a green cell border
enterrun cell, select below
enterrun cell, insert below
Ainsert cell above
Binsert cell below
Ddelete selected cell
Mmerge selected cells, or current cell with cell below if only one cell selected
0restart kernel (with dialog)
Ychange cell to
Mchange cell to
markdownmode (good for documentation)
clickfor multi-cursor editing
scrolling clickfor column editing
/toggle comment lines
tabcode completion or indent
Want quick access to all the commands in Jupyter Notebooks? Open the command palette with
p and you’ll quickly be able to search all the commands!
View all keyboard shortcuts
H (in Command mode)
Forget what that keyboard shortcut is? Type
H in Command mode for a list of all available keyboard shortcuts.
Text snippets allow me to save time typing and keep things consistent.
For my text snippets, I use Textexpander which is Mac OSX only. However, for Windows I’ve used PhraseExpress in the past which works well too.
Quick imports for all your favorite packages
Constantly importing the same packages and/or forget what that package you always use is named? I like to store my default imports in a snippet such as the following. I’d recommend you create a similar snippet and tune it to your preferences.
from __future__ import division import numpy as np import pandas as pd from pandas import Series, DataFrame from numpy.random import randn from scipy import stats import matplotlib as mpl import matplotlib.pyplot as plt import seaborn as sns sns.set_style('whitegrid') %matplotlib inline import math from sklearn.linear_model import LogisticRegression from sklearn.linear_model import LinearRegression from sklearn.cross_validation import train_test_split from sklearn import metrics import statsmodels.api as sm from pprint import pprint
Making writing functions and documentation less painful
I like to remind myself to write a function DocString every time I write a function by using the following snippet.
def (): ''' '''