Python for Data course menu
Jupyter Notebooks for Beginners: Cells, Shortcuts and Best Practices
Learn to use Jupyter notebooks for data analysis: code and Markdown cells, running cells, how the last expression is displayed, the execution order trap, magic commands and keyboard shortcuts.
On this page
A Jupyter notebook is a document made of cells. Code cells run Python and show their results right below; Markdown cells hold notes, headings and explanations. Charts and tables appear inline. That makes notebooks ideal for exploring data step by step and for sharing an analysis with its reasoning.
Notebook files end in .ipynb. JupyterLab, VS Code, PyCharm, Google Colab and Kaggle can all open them.
Cells#
- Code cells run Python. Press Shift + Enter to run a cell and move to the next one.
- Markdown cells hold formatted text: headings (
#), lists, bold, links and tables. - The number in
[3]:next to a cell shows the order cells were run in.
The last expression is displayed#
In a notebook you rarely need print: the value of the last line of a cell is displayed automatically, and pandas tables appear as formatted HTML tables. Outside a notebook (in a .py script), nothing is shown unless you print it, which is why the lessons in this course use print(): it works the same everywhere.
import pandas as pd
df = pd.DataFrame({"name": ["Asha", "Ravi"], "score": [91, 78]})
print(df) name score
0 Asha 91
1 Ravi 78In a notebook, a cell containing just df on its last line would show the same table, nicely formatted.
The execution order trap#
Cells can run in any order, and variables stay in memory between runs. That is convenient, and also the most common source of confusing notebooks:
x = 10 # cell Ax = x * 2 # cell B: run it twice and x becomes 40, not 20A notebook that only works because cells were run in a special order will break for anyone else. Before sharing, use Kernel → Restart Kernel and Run All Cells. If it runs top to bottom without errors, it is reproducible.
Magic commands#
Lines starting with % are IPython magic commands, special to notebooks:
%pip install is safer than !pip install because it always targets the Python the notebook is running.
Keyboard shortcuts#
Press Esc to enter command mode (the cell is selected, not edited), then:
Good habits#
- Put all
importstatements in the first cell. - Use Markdown cells to explain why, not only what.
- Restart and run everything before sharing or drawing conclusions.
- Move code you reuse into a
.pyfile and import it. - Keep raw data files unchanged; save cleaned data under a new name.
Key takeaway#
Next, the foundation of numerical Python: NumPy Arrays.
Finished this lesson?