Introduction to Jupyter Notebooks

We are going to review core functionalities of Jupyter notebooks.

~ Expert users may skip this introductory tutorial! ~

Execute Python code

The folowing cell will execute a simple python command.
In order to execute the cell:

  • Click the cell
  • In [ ]:
    print("Hello World!!!")

    The output generated by the Python command is reported right after the cell

    Execute BASH code

    If instead of Python you want to use BASH: the cell must begin with the header line %%bash. In this way the whole cell is interpreted as BASH.

    In [ ]:
    echo "Hello World!!!"
    echo "Any other line is again BASH!"

    Execute both Python and BASH

    In order to mix Python and BASH, start each BASH line with the character "!", and leave Python as it is.
    Comments start with "#".

    In [ ]:
    # bash (this is a comment)
    !echo "I love this school and this is BASH!"
    # python
    print("I love this school and this is Python!")

    Where does the Jupyter notebook run?

    Jupyter Notebooks is simply invoking commands that are executed in your local directory.

    In [ ]:
    # where are we? show the local directory
    In [ ]:
    # list content of the local directory 

    Download a file from the web, give it a look and delete it

    Let's download the file
    from the URL

    In [ ]:
    # download a file
    !wget -N

    Let's give a quick look at the file Si_ONCV_PBE-1.1.upf

    In [ ]:
    # give a look
    !cat Si_ONCV_PBE-1.1.upf

    Let's remove the file Si_ONCV_PBE-1.1.upf

    In [ ]:
    # remove the file 
    !rm Si_ONCV_PBE-1.1.upf
    # list content of the local directory 

    Write/Read a text file

    We review how I/O operations with text files work in Python.

    In [ ]:
    # open the file hello.dat in write mode
    with open("hello.dat", "w") as file :
        file.write("Hello!!") # write
    In [ ]:
    # list content of the local directory 
    # give a quick look at the file 
    !cat hello.dat 
    In [ ]:
    # open the file hello.dat in read mode 
    with open("hello.dat", "r") as file :
        data = # read 
    # data holds the content read from the file
    print(data) # show 
    In [ ]:
    # remove the file 
    !rm hello.dat

    Convert Python data structures to/from a JSON file

    We show how Python data structures can be easily dumped in/loaded from a JSON file.

    Let's create a Python data structure.

    In [ ]:
    # a dictionary contains keys and values
    # it can be nested 
    # lists contain information which can be iterated
    # show the data 

    Write the Python data in the JSON file data.json

    In [ ]:
    import json
    # write data to JSON file 
    with open('data.json', 'w') as file:
        json.dump(data, file)

    Give a quick look at the file data.json

    In [ ]:
    # give a quick look at the file 
    !cat data.json

    Read the Python data from the JSON file data.json

    In [ ]:
    import json
    # read data from JSON file 
    with open('data.json',"r") as file:
        d = json.load(file)
    # show the data 
    # pretty print the data
    print(json.dumps(data, indent=2))
    In [ ]:
    # remove the file 
    !rm data.json

    Generate data and create a simple plot

    We are going to generate the (X,Y) data points for the functions: $Y=X$, and $Y=X^2$, then plot the data points.

    Generate data

    In [ ]:
    import numpy as np
    # generate 6 points equally spaced from 0 to 3.
    x = np.linspace(0,3,6) 
    # generate y points for each x (implicit iteration)
    y1 = x
    y2 = x*x
    # show data
    print("x  : ", x)
    print("y1 : ", y1)
    print("y2 : ", y2)

    Plot data

    In [ ]:
    import matplotlib.pyplot as plt
    # plot 
    fig, ax = plt.subplots(1, 1)
    ax.plot(x, y1, 'bo-', label="$y=x$" )
    ax.plot(x, y2, 'ro-', label="$y=x^2$" )
    # set labels 
    # set legend
    # set title 
    # show the plot

    Hi 5! You are no longer a toddler!

    This website does not host notebooks, it only renders notebooks available on other websites.

    Delivered by Fastly, Rendered by Rackspace

    nbviewer GitHub repository.

    nbviewer version: 38e181a

    nbconvert version: 5.3.1

    Rendered (Tue, 22 May 2018 13:56:14 UTC)