{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
We are going to review core functionalities of Jupyter notebooks.
\n", "~ Expert users may skip this introductory tutorial! ~
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The folowing cell will execute a simple python command.
\n",
"In order to execute the cell:\n",
"
The output generated by the Python command is reported right after the cell
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "echo \"Hello World!!!\"\n", "echo \"Any other line is again BASH!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to mix Python and BASH, start each BASH line with the character \"!\", and leave Python as it is.
\n",
"Comments start with \"#\".
Jupyter Notebooks is simply invoking commands that are executed in your local directory.
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# where are we? show the local directory\n", "!pwd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# list content of the local directory \n", "!ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's download the file
\n",
"Si_ONCV_PBE-1.1.upf
\n",
"from the URL 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
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# give a look\n", "!cat Si_ONCV_PBE-1.1.upf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Let's remove the file Si_ONCV_PBE-1.1.upf
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# remove the file \n", "!rm Si_ONCV_PBE-1.1.upf\n", "\n", "# list content of the local directory \n", "!ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
We review how I/O operations with text files work in Python.
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# open the file hello.dat in write mode\n", "with open(\"hello.dat\", \"w\") as file :\n", " file.write(\"Hello!!\") # write" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# list content of the local directory \n", "!ls \n", "\n", "# give a quick look at the file \n", "!cat hello.dat " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# open the file hello.dat in read mode \n", "with open(\"hello.dat\", \"r\") as file :\n", " data = file.read() # read \n", "\n", "# data holds the content read from the file\n", "print(data) # show " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# remove the file \n", "!rm hello.dat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We show how Python data structures can be easily dumped in/loaded from a JSON file.
\n", "Let's create a Python data structure.
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# a dictionary contains keys and values\n", "data={}\n", "data[\"project\"]=\"liquid_project\"\n", "\n", "# it can be nested \n", "data[\"material\"]={}\n", "data[\"material\"][\"name\"]=\"water\"\n", "data[\"material\"][\"formula\"]=\"H2O\"\n", "data[\"material\"][\"density\"]=1\n", "\n", "# lists contain information which can be iterated\n", "data[\"material\"][\"atomic_species\"]=[\"O\",\"H\"]\n", "\n", "# show the data \n", "print(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write the Python data in the JSON file data.json" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "\n", "# write data to JSON file \n", "with open('data.json', 'w') as file:\n", " json.dump(data, file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Give a quick look at the file data.json
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# give a quick look at the file \n", "!cat data.json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read the Python data from the JSON file data.json" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "\n", "# read data from JSON file \n", "with open('data.json',\"r\") as file:\n", " d = json.load(file)\n", "\n", "# show the data \n", "print(d) \n", "# pretty print the data\n", "print(json.dumps(data, indent=2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# remove the file \n", "!rm data.json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
We are going to generate the (X,Y) data points for the functions: $Y=X$, and $Y=X^2$, then plot the data points.
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generate data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "# generate 6 points equally spaced from 0 to 3.\n", "x = np.linspace(0,3,6) \n", "\n", "# generate y points for each x (implicit iteration)\n", "y1 = x\n", "y2 = x*x\n", "\n", "# show data\n", "print(\"x : \", x)\n", "print(\"y1 : \", y1)\n", "print(\"y2 : \", y2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "# plot \n", "fig, ax = plt.subplots(1, 1)\n", "ax.plot(x, y1, 'bo-', label=\"$y=x$\" )\n", "ax.plot(x, y2, 'ro-', label=\"$y=x^2$\" )\n", "\n", "# set labels \n", "plt.xlabel('x')\n", "plt.ylabel('y')\n", "\n", "# set legend\n", "plt.legend()\n", "\n", "# set title \n", "plt.title(\"Functions\")\n", "\n", "# show the plot\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "