#!/usr/bin/env python # coding: utf-8 # # Notebook basics # # This notebook covers the minimal information one needs in order to execute code in an existing notebook. # If you already know notebooks even a tiny bit, no need to dwell here, you won't learn anything new :-) # # # --- # # - [What is a notebook?](#What-is-a-notebook?) # - [Executing code cells](#Executing-code-cells) # - [State sharing and persistence](#State-sharing-and-persistence) # - [Creating new cells](#Creating-new-cells) # - [Going further](#Going-further) # - [Generic tutorials](#Generic-tutorials) # - [Useful pages from the official Jupyter documentation](#Useful-pages-from-the-official-Jupyter-documentation) # # --- # # # ## What is a notebook? # # > A notebook integrates code and its output into a single document that combines visualizations, # narrative text, mathematical equations, and other rich media. # In other words: it's a single document where you can run code, display the output, and also add explanations, # formulas, charts, and make your work more transparent, understandable, repeatable, and shareable. # # --- from [How to Use Jupyter Notebook in 2020: A Beginner’s Tutorial](https://www.dataquest.io/blog/jupyter-notebook-tutorial/) # # That's about it. A notebook is a mean to interleave executable code with some documentation, which can be text, image... # pretty much anything! # Notebooks support [many programming languages](https://github.com/jupyter/jupyter/wiki/Jupyter-kernels). In this tutorial, we write Python code. # # The notebook content is divided into _cells,_ which are independent block of either code or documentation. # - The text you read now has been written in a markdown cell (documentation) # - Right below, you can see a simple code cell containing only a `print` statement. # In[ ]: print("Hi there, I'm a code cell!") # As you can see, the rendering of the code cell is different. # (Note: the exact rendering depends on the software used to visualize and interact with the notebook) # # One important difference with the documentation cell is the `[ ]` showing up on the left-side of the cell. # While the brackets are empty, it indicates that the code in that cell has not been executed yet. # # ## Executing code cells # # Let go ahead and execute it! # - Click on the code cell above (or navigate to it with the keyboard up and down arrows); # - Execute the code by either # - Clicking the `Run` button from the task bar, or # - Using the Ctrl+Enter keyboard shortcut. # # Tada! You can now see the output of your code cell displayed below the cell, if any. # In our case, the output is a simple string. # # Notice that the brackets now indicate `[1]`. If you execute the cell again, it will show `[2]`, and so on. # Thus, as you might have got it already, the number in the brackets indicate the sequence number of the cell execution; # every time a cell (any cell) is executed in the notebook, this number is incremented and displayed next to the cell. # # > **Note.** The same commands also render documentation cells. # E.g., if you edit a Markdown cell (i.e., you see the source code displayed), you can render it with Ctrl+Enter. # # ## State sharing and persistence # # One important observation is that the code cell share their state! # That is, variables or functions that are created in one executed cell can be used in other cells. # # Let illustrate that real quick. # In[ ]: # Let's instantiate a variable here... myVar = 42 # Once the cell above has been executed, `myVar` is globally defined and accessible anywhere in the notebook. # In[ ]: # ... we can access it there print(myVar) # Finally, state is _persistent,_ that is, even if you change the content of a cell and re-execute it, # the output of past executions are still kept in memory. For example, if you update the cell above # to change the variable name from `myVar` to `my_var` and re-execute, a new variable will be created, # but **the old one remains too!** # # Try-it out, you'll see you can still run `print(myVar)`. # # If you want to clean up the state of a notebook, you can either # - restart the kernel by clicking the circled arrow in the task bar, or # - go to `Kernel` and select `Restart` or `Restart & Clear Output`. # ## Creating new cells # # At some point, you may want to create new cells. There are many ways to do this, # here are some of the most convenient ones: # - Click the `+` button in the task bar (`Insert cell below`); # - Use the Alt+Enter shortcut, which both executes the current cell and inserts a new cell below. # ## Going further # # This was a really minimalistic introduction into how to interact with a Jupyter notebook. # Naturally, it barely scratched the surface of what there is to know about notebooks! # # If you want to dig a little deeper, there are plenty of good tutorials for beginners throughout the web. # Here is a short selection. # # ### Generic tutorials # - [How to Use Jupyter Notebook in 2020: A Beginner’s Tutorial](https://www.dataquest.io/blog/jupyter-notebook-tutorial/) # - [Jupyter Notebook Tutorial: The Definitive Guide](https://www.datacamp.com/community/tutorials/tutorial-jupyter-notebook) # # ### Useful pages from the official Jupyter documentation # - [Notebook user interface and Document structure](https://jupyter-notebook.readthedocs.io/en/stable/notebook.html#notebook-user-interface) # - [Basic wokflow](https://jupyter-notebook.readthedocs.io/en/stable/notebook.html#basic-workflow) # - [User interface components](https://jupyter-notebook.readthedocs.io/en/stable/ui_components.html) # --- # Next step: [Data Analysis](live_exp-sizing.ipynb) # [Back to repo](.)