0% found this document useful (0 votes)
3 views10 pages

Interactive Python Notebook Tips

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Interactive Python Notebook Tips

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Interactive Python Notebooks

1. What are interactive Python notebooks?


An interactive Python notebook is where you can write and run Python code, add explanations,
and create visualizations—all in one place. Think of it as a digital lab notebook for coding!

Why Use a Notebook?

●​ Interactivity: Run code step-by-step and see results instantly.


●​ Documentation: Mix code with text to tell a story.
●​ Visuals: Add charts and plots effortlessly.
●​ Sharing: Share your work with friends, colleagues, or the world.

2. Understanding the notebook interface


A Jupyter Notebook is made up of cells—little boxes where you write stuff. There are two main
types: markdown text cells and code cells:

●​ Markdown text cells: These are for text. Use them to write explanations, headings, lists,
or even add links and images. Think of them as your storytelling tool.

●​ Code cells: These are where the magic happens—write and run Python code here.

3. Your first Markdown cell


Let’s try it out!

Click the "+" button in the toolbar to add a new text cell.
Type this:

Depending on your setup, hit "Run" (the play button) or press Shift + Enter. Boom! The text
formats beautifully in the cell.

Markdown keeps your notebook readable and organized.

4. Writing and running code


Now, let’s run some Python!

Click the "+" button in the toolbar to add a new code cell.

Type this:

Depending on your setup, hit "Run" (the play button) or press Shift + Enter. That code ran!
Each code cell is like a mini-program. When you "run" it, Python executes the code inside and
shows the output right below.

5. Running code cells


You can run cells one at a time or in any order. To run a cell:

●​ Select the cell you want to run.


●​ Press Shift + Enter or click the "Run" button on the cell.

To run all cells in sequential order, Use the "Run all" option in the menu.
6. Reordering cells
Sometimes, your notebook needs a little reorganization.

To reorder a cell, click on that cell, then click on the up or down arrow. Here we clicked on the
second cell, then clicked on the "Move cell up" arrow.

Now the second cell has been moved to the first position.
7. Variables in notebooks
Variables are a big deal in notebooks because they "stick around" after you define them. Let’s
experiment.

In two separate code cells, type the following then run both cells:

Nothing prints under the first cell because it only assigns the value of 10 to x.

The second cell outputs 10 because it retrieves and prints the value of x. This demonstrates
that the variable x is stored in your notebook’s session and remains accessible across cells.

In addition, the order that the cells are run matters and may not match their physical order on
the page. Each cell has an execution counter in brackets next to it, indicating the sequence in
which it was run, not its position in the notebook. Consider this example:

In this case, the output of print(x) is 5, not 10, because the most recent assignment to x (from
Cell 3) overwrites the earlier value. This highlights how the order that the cells are run, tracked
by the counters, determines the final value of persistent variables like x.
8. Rerunning cells
The first cell below loads a csv with two columns: 'sport' and 'number_of_players'. The
second cell filters the dataframe to show only rows where the 'number_of_players' equals 15.
The third cell renames the column 'number_of_players' to 'num_players'.

What would happen if we rerun the second cell?


We now encounter an error!

The error message will include KeyError: 'number_of_players'.

Why does this happen? After renaming the column in Cell 3, 'number_of_players' no longer
exists in df. It is now 'num_players'.

Key Takeaway: variables including dataframes persist across cells in a Python notebook
session. Their state reflects the most recent executed changes.
9. Resetting runtime
Occasionally, you may need to reset your runtime in a Python notebook. Resetting clears all
variables stored in the current session, giving you a fresh start. Let’s walk through an example.

In two separate code cells, enter and run the following code:

Now, go back to the first cell and modify it by replacing 'length' with 'l' and 'width' with 'w'.

Run both cells again.

The code above has an error since 'length' and 'width' are not defined. Surprisingly, the output of
the second cell remains 50. Why does this happen?

The notebook session still remembers the original variables 'length' and 'width' from the first run,
and the second cell continues to use them to compute the area.
To catch errors like this, you sometimes need to reset the runtime.

Now there are no variables stored by the notebook session. Also note that execution counter
brackets are empty.

Now if we rerun the cells, we see the error in our code.


10. Opening a notebook
When you open a Python notebook, it may appear as though the code has already been
executed. For instance, you might see outputs like df.head(3) displayed below a cell. However,
the execution counter brackets—typically showing a number like [1]—are empty, indicating
that the session’s variables have been cleared.

If you attempt to run a later cell, such as the second one, without first running the initial cell,
you’ll encounter an error like NameError: name 'df' is not defined. This happens because the
notebook hasn’t yet defined the variables in the current session.

To resolve this, simply rerun the cells in sequential order.

You might also like