Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 39 ยท Projects and Practice

Practice Questions and Challenges

Test what you learned about reproducible notebooks, JupyterLab, sharing, testing and security with a quiz, short exercises, a runnable checker and review prompts.

You have covered environments, hidden state, seeds, JupyterLab, extensions, version control, nbconvert, Papermill, sharing, Voila, structure, modules, testing, performance and security. This lesson is a workout: a mixed quiz, hands-on exercises with runnable checkers, and a review checklist for your own notebooks.

Jupyter itself is not available on this site, so the exercises are written to be solved in ordinary Python where you can run them here, and as paper exercises (commands and decisions) where the real tool is needed. Try each exercise before opening its solution.

Mixed quiz

Quick check ยท 8 questions

of 8 answered You scored / 8 ยท perfect!

  1. 1. A notebook works in your session but fails after Restart and Run All. What is the most likely cause?

    A fresh kernel has no leftover names, so it exposes dependencies on cells that no longer run first.
  2. 2. Which line installs a package into the environment the notebook is actually using?

    The %pip magic targets the running kernel's Python.
  3. 3. Which tool gives a cell-aware diff and merge for notebooks?

    nbdime understands the JSON structure and diffs by cell.
  4. 4. You want to run one notebook for 12 months with different dates. Which tool fits?

    Papermill injects parameters and runs the notebook once per set of values.
  5. 5. Which sharing option needs no running server at all?

    A static export is just a file that anyone can open.
  6. 6. Which is the safest way to use an API key?

    Environment variables keep secrets out of the notebook file and its outputs.
  7. 7. What does np.random.default_rng(7) give you?

    A seeded generator is deterministic and local, which keeps results repeatable.
  8. 8. Which keeps a Jupyter server on a remote machine private?

    The server stays bound to localhost, and the tunnel gives you encrypted access.

Exercise 1: spot the hidden state

Here is a toy notebook as a list of cells, with the order in which a user ran them. Write a function depends_on_deleted(cells, executed) that returns the names a cell uses that no remaining cell defines. Solve it first, then check.

Python Exercise 1: find undefined names (starter)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The checker reports rate in cell 2. A real tool would also look at definition order (a name used before its definition is a problem even if defined later). Extend it: only count a name as defined if a cell above defines it.

Exercise 2: a seed audit

Predict, without running, which of the following prints the same list twice, then run to check.

Python Exercise 2: which runs are repeatable?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

a and c are repeatable, b is not (it continues the global stream), and c equals a because Random(5) and random.seed(5) start the same stream. Writing a short sentence explaining why is part of the exercise.

Exercise 3: write a requirements checker

Given a requirements text and a dictionary of installed versions, list packages that are missing or not pinned with ==.

Python Exercise 3: requirements checker (complete it)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output flags numpy>=2.0 and requests as unpinned and matplotlib as the wrong version. Improve it: parse >= lines, report unpinned names without crashing on numpy>=2.0 (which currently prints the full line), and compare versions numerically instead of as text.

Exercise 4: paper exercises (commands and decisions)

These need the real tools, so write your answers on paper or in a text file, then compare with the lessons.

  1. Write the commands to create a virtual environment named .venv, install jupyterlab and pandas, and register it as a kernel called "Python (sales)". See reproducible environments.
  2. Your colleague's notebook diff shows 4,000 changed lines after a re-run. Name two fixes and the tools for each. See version control.
  3. Write the Papermill command that runs report.ipynb for region south and month 2024-04 into out/south.ipynb. Then write the nbconvert command that turns that file into HTML without code. See Papermill and nbconvert.
  4. A class of 120 students needs a persistent notebook environment for a semester. Which sharing option fits, and which two settings protect the server? See sharing.
  5. A teammate pastes an unknown notebook into your repository. List four things you check before running it. See notebook security.

Model answers, in brief: (1) python -m venv .venv, activate, pip install jupyterlab pandas ipykernel, python -m ipykernel install --user --name sales --display-name "Python (sales)". (2) nbstripout or Jupytext, with nbdime for review. (3) papermill report.ipynb out/south.ipynb -p region south -p month 2024-04 and jupyter nbconvert --to html --no-input out/south.ipynb. (4) JupyterHub (a Kubernetes deployment at that size), with resource limits and HTTPS plus a trusted authenticator. (5) imports and installs, os/subprocess/network calls, hard-coded paths and credentials, and large or suspicious outputs; run it in isolation.

Exercise 5: a mini linter rule

Write a rule that flags code cells whose first line is not an import when the notebook's earlier cells already contain a non-import statement, i.e. an "imports not at the top" check.

Python Exercise 5: imports at the top (starter)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

It reports cell 3. Now decide: what should happen with a cell that mixes an import and other statements? (Hint: the rule above treats it as "other code".) Compare your decision with the rule list in the linter project.

Review checklist for your own notebook

  • Does it have a title, a purpose and headings?
  • Does Restart and Run All finish without errors, and in reasonable time?
  • Are versions pinned and the environment documented?
  • Are seeds and paths defined once at the top?
  • Is reusable logic in a tested module, with the notebook thin?
  • Are outputs stripped or deliberately kept, and free of secrets and personal data?
  • Could a stranger run it from the README alone?

Where to go next

Pick a real notebook of your own and apply the checklist, then try the sales report project on your data. Deepen the data side with the pandas and NumPy tutorials on this site, and the Python tutorial if functions, modules or testing still feel shaky.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. What is the first thing to try when a notebook gives surprising results?

    A clean top-to-bottom run removes hidden state, which explains a large share of surprises.
  2. 2. Which habit best improves a notebook's maintainability over time?

    Modules can be tested, reviewed and reused; notebooks then explain and display.
  3. 3. A result must be reproduced a year later. What do you need besides the notebook?

    Environment, randomness and data are the three inputs that decide the result.

Summary

  • Hidden state, unpinned environments, unseeded randomness and secrets in outputs are the recurring problems.
  • Fixes are habits: restart and run all, pin and record, seed locally, test functions, scan outputs.
  • Choose sharing by need: static export, Binder, JupyterHub or a hosted service; use Papermill for repeated runs.
  • Reuse the checklist on every notebook you ship.

Next: back to the Jupyter and Notebooks track page