Cells and Executing Code
In this activity, we take a closer look at one of the defining features of Jupyter Notebooks: the use of cells. A notebook is organised into a sequence of individual cells stacked one after another, each of which can be edited, run, moved, or deleted independently of the others. This cell-based structure is what allows you to combine explanatory text with executable Python code within a single interactive document, for example, a text cell might introduce the concept of a loop, followed immediately by a code cell demonstrating one in action, with its output displayed right below it. Because each cell operates as its own discrete unit, you can run them in any order you like, rerun just one cell after making a small change without needing to execute the whole notebook again, or insert a new cell partway through to test an idea, all of which makes cells a flexible and central building block of how notebooks work.
Watch video: Jupyter Notebook Cells and Executing Code
Text Cells / Markdown Cells
Text cells, also known as Markdown cells, are used to document and narrate your work rather than to execute code. They rely on Markdown, a lightweight formatting syntax, to produce formatted text, including headings and subheadings for structuring sections, bullet points or numbered lists for breaking down steps, hyperlinks for pointing to external resources or references, tables for presenting information in rows and columns, mathematical equations written using LaTeX notation for anything from a simple formula to a more elaborate expression, and embedded images for illustrating a concept or showing a diagram. For example, a Markdown cell might use a heading to introduce a new section, a short paragraph to explain the goal of the upcoming code, and a bulleted list summarising the steps that follow.
These cells play an important role in making a notebook understandable, since they let you explain the purpose behind a piece of code, note any assumptions or caveats, or summarise a result, all in plain, readable language rather than as code comments. This is what makes notebooks considerably easier to read, follow, and share with others, whether that's a fellow student trying to understand your workings, a colleague reviewing your analysis, or your future self returning to the notebook months later and needing a reminder of what each part does and why.
Code Cells
Code cells contain Python commands that can be executed by the notebook's Python kernel. When a code cell is run, the output, such as text, tables, figures, or error messages, is displayed immediately below the cell, providing instant feedback.
Executing Code
A code cell can be executed by selecting it and pressing Shift + Enter, or by clicking the Run button on the toolbar. Executing a cell sends its contents to the Python interpreter, which processes the commands and returns the corresponding output.
Why Cells Are Useful
The cell-based structure allows programs to be developed incrementally. You can edit and rerun individual sections of code without executing the entire notebook, making experimentation, debugging, and documentation much more efficient.
In the next activity, we explore text cells (Markdown cells), which are used to document and organise a notebook alongside its code.
Jupyter Notebooks: Text Cells
In this activity, we explore Markdown cells, also known as text cells. These cells allow you to add explanations, headings, notes, equations, and other documentation alongside your Python code, making notebooks easier to understand and share.
What is a Markdown Cell?
A Markdown cell is used to write formatted text rather than executable Python code. It enables you to describe your analysis, provide instructions, document results, and organise your notebook into clearly defined sections.
Formatting Features
Markdown supports a variety of formatting options, including headings, bold and italic text, bullet and numbered lists, hyperlinks, tables, images, block quotations, and mathematical equations. These features help create well-structured and professional notebooks.
Why Use Text Cells?
Text cells improve the readability and organisation of a notebook by documenting the purpose of each section of code, explaining methods and assumptions, and interpreting results. They are an important component of reproducible research and collaborative programming.
Editing Markdown Cells
A Markdown cell can be edited like any text document. Once the content has been written, pressing Shift + Enter renders the formatted output, allowing you to view the document as it will appear in the notebook.