#!/usr/bin/env python # coding: utf-8 #

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
  • #
  • Press SHIFT+ENTER
  • # #

    # 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[ ]: get_ipython().run_cell_magic('bash', '', 'echo "Hello World!!!"\necho "Any other line is again BASH!"\n') #

    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) get_ipython().system('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 get_ipython().system('pwd') # In[ ]: # list content of the local directory get_ipython().system('ls') #

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

    #

    Let's download the file
    # Si_ONCV_PBE-1.1.upf
    # from the URL http://www.quantum-simulation.org/potentials/sg15_oncv/upf/Si_ONCV_PBE-1.1.upf

    # In[ ]: # download a file get_ipython().system('wget -N http://www.quantum-simulation.org/potentials/sg15_oncv/upf/Si_ONCV_PBE-1.1.upf') #

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

    # In[ ]: # give a look get_ipython().system('cat Si_ONCV_PBE-1.1.upf') #

    Let's remove the file Si_ONCV_PBE-1.1.upf

    # In[ ]: # remove the file get_ipython().system('rm Si_ONCV_PBE-1.1.upf') # list content of the local directory get_ipython().system('ls') #

    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 get_ipython().system('ls') # give a quick look at the file get_ipython().system('cat hello.dat') # In[ ]: # open the file hello.dat in read mode with open("hello.dat", "r") as file : data = file.read() # read # data holds the content read from the file print(data) # show # In[ ]: # remove the file get_ipython().system('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 data={} data["project"]="liquid_project" # it can be nested data["material"]={} data["material"]["name"]="water" data["material"]["formula"]="H2O" data["material"]["density"]=1 # lists contain information which can be iterated data["material"]["atomic_species"]=["O","H"] # show the data print(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 get_ipython().system('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 print(d) # pretty print the data print(json.dumps(data, indent=2)) # In[ ]: # remove the file get_ipython().system('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 plt.xlabel('x') plt.ylabel('y') # set legend plt.legend() # set title plt.title("Functions") # show the plot plt.show() #

    Hi 5! You are no longer a toddler!

    # In[ ]: