We are going to review core functionalities of Jupyter notebooks.
~ Expert users may skip this introductory tutorial! ~
The folowing cell will execute a simple python command.
In order to execute the cell:
print("Hello World!!!")
The output generated by the Python command is reported right after the cell
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.
%%bash
echo "Hello World!!!"
echo "Any other line is again BASH!"
In order to mix Python and BASH, start each BASH line with the character "!", and leave Python as it is.
Comments start with "#".
# bash (this is a comment)
!echo "I love this school and this is BASH!"
# python
print("I love this school and this is Python!")
Jupyter Notebooks is simply invoking commands that are executed in your local directory.
# where are we? show the local directory
!pwd
# list content of the local directory
!ls
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
# download a file
!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
# give a look
!cat Si_ONCV_PBE-1.1.upf
Let's remove the file Si_ONCV_PBE-1.1.upf
# remove the file
!rm Si_ONCV_PBE-1.1.upf
# list content of the local directory
!ls
We review how I/O operations with text files work in Python.
# open the file hello.dat in write mode
with open("hello.dat", "w") as file :
file.write("Hello!!") # write
# list content of the local directory
!ls
# give a quick look at the file
!cat hello.dat
# 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
# remove the file
!rm hello.dat
We show how Python data structures can be easily dumped in/loaded from a JSON file.
Let's create a Python data structure.
# 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
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
# give a quick look at the file
!cat data.json
Read the Python data from the JSON file data.json
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))
# remove the file
!rm data.json
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
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
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()