2 + 2
4
Computational notebooks provide an interactive environment for combining code, its results, and explanatory text. They are particularly well-suited for iterative tasks such as data analysis, as they allow you to test and document your work step-by-step. Features like automatic setup, immediate feedback, and the ease of experimentation make notebooks an accessible way to learn Python, especially when compared to more traditional programming approaches.
Google Colab is a cloud-based platform that lets you run notebooks without any local installation. This makes it useful for quickly exploring datasets or prototyping analytical workflows.
.ipynb
file via “File > Upload Notebook.”A notebook is made up of cells, which can contain either executable code or formatted text.
To run a code cell, select it and then either press Shift+Enter
or click the play button. Here’s an example:
The output appears below the cell.
Another example:
A handy feature of notebooks is that the value of the last expression in a code cell is automatically displayed as output:
If you need to display the values of other variables, or variables that aren’t on the last line of a cell, use the print()
function:
Notebooks are designed for iterative work. You can easily update and rerun code cells to see the impact of your changes. This flexibility is a core advantage of using notebooks.
In an active notebook session, variables defined in one cell remain available in subsequent cells. This allows later cells to build upon the work of earlier ones. While this is a powerful feature, it’s important to be aware that running cells out of sequence, or changing earlier cells without rerunning those that depend on them, can lead to confusion or errors.
As mentioned, variables you define in one cell can be accessed by other cells that are run later:
Once the city_name
variable is defined by running its cell, other cells can use it without needing to define it again.
If the defining cell is not run, a subsequent cell using city_name
will raise an error.
If you change the value of a variable, you must rerun the cell where it’s defined to update its value in the notebook’s memory.
Subsequent cells will then use the updated variable:
The order in which you execute cells is crucial as it determines the notebook’s current state. Running cells out of their logical order can lead to errors or unexpected outcomes.
To avoid such issues:
The execution order directly affects the notebook’s environment. Variables, functions, and imported libraries become available only after their respective defining cells have been executed. For instance:
The x
variable is now defined and available for subsequent cells:
If x
’s value is changed in its defining cell, dependent cells must be rerun to use the new value:
The third cell will fail if the preceding cell (defining y
) has not been run. Similarly, if the cell defining y
is modified (e.g., y = x * 3
), the cell using z
must be rerun. Otherwise, z
will be based on an outdated y
.
Text cells use Markdown, which is a lightweight markup language. Markdown employs simple characters for formatting, enabling you to create headings, bold or italic text, lists, and tables. For a comprehensive guide, please refer to the linked documentation.