{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Introducing python and the ipython notebook\n", "===========================================\n", "\n", "Even if many tools of TRIQS are coded in C++, they usually have a user interface written in\n", "python. This means that everything is controlled by python scripts, pre- and post-processing of\n", "the data, choice of DMFT scheme, plotting, etc. We chose to use python because it is a very\n", "simple and intuitive language. Being an interpreted language, there is never a need to recompile\n", "code if you decide to change variables, add some extra pieces of data analysis, etc.\n", "\n", "In the following we will provide a brief overview of some core aspects of the language.\n", "For a thorough introduction to Python for scientific data analysis we recommend these resources:\n", "\n", "* https://swcarpentry.github.io/python-novice-gapminder\n", "* https://swcarpentry.github.io/python-novice-inflammation\n", "\n", "Python can be used either in interactive or in script mode.\n", "\n", "Script mode\n", "-----------\n", "\n", "In script mode, you edit a file (with an extension .py), say my_script.py, and run it\n", "with python. You do this from a shell:\n", "\n", "```>>> python my_script.py```\n", "\n", "Interactive shell mode\n", "----------------------\n", "\n", "From a shell, you can also directly type\n", "\n", "```>>> ipython```\n", "\n", "This will bring you in interactive mode. You can type commands one after the other\n", "and they are executed on the fly.\n", "\n", "The ipython notebook\n", "--------------------\n", "\n", "This is what you are looking at right now. It is also an interactive mode, with all your\n", "commands appearing in a \"mathematica\"-like notebook. The advantage, as we will see,\n", "is that the result of commands, even plots, directly appears and stays in the notebook.\n", "\n", "Let us start with a first simple code that we will execute in the notebook. To do\n", "so, click on the input line below and execute it with Shift+Enter" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2023-08-28T15:03:26.887976Z", "iopub.status.busy": "2023-08-28T15:03:26.887691Z", "iopub.status.idle": "2023-08-28T15:03:26.897518Z", "shell.execute_reply": "2023-08-28T15:03:26.897112Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world!\n" ] } ], "source": [ "print(\"Hello world!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see the result of the command appears above.\n", "\n", " *Exercise*\n", "----------\n", "\n", "As an exercise, run this same command\n", "\n", "- using the script mode (edit a my_script.py file, add the print line above and execute the file from a shell)\n", "- in interactive shell mode (you quit the interactive mode with Ctrl+d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A very short introduction to python\n", "====================================\n", "\n", "The quickest way to learn python is certainly to follow the tutorial at the following link:\n", "\n", "- http://docs.python.org/2/tutorial/\n", "\n", "We will quickly show some examples that should be self-explanatory.\n", "\n", "\n", "Calculating\n", "-----------" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2023-08-28T15:03:26.925726Z", "iopub.status.busy": "2023-08-28T15:03:26.925595Z", "iopub.status.idle": "2023-08-28T15:03:26.927821Z", "shell.execute_reply": "2023-08-28T15:03:26.927564Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "first result: 2.3095238095238093\n", "i^2: (-1+0j)\n", "complex calculation: (16+12j)\n" ] } ], "source": [ "# Comments start with a #\n", "\n", "# Setting variables and doing calculations\n", "x = 3\n", "y = 6.7\n", "print('first result: ', (x+y)/4.2)\n", "\n", "# Using complex numbers\n", "i = 1j\n", "print('i^2: ', i*i)\n", "print('complex calculation: ', (2+4j)*(4-2j))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simple loops / indentation\n", "-------------------------" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-08-28T15:03:26.929314Z", "iopub.status.busy": "2023-08-28T15:03:26.929209Z", "iopub.status.idle": "2023-08-28T15:03:26.931138Z", "shell.execute_reply": "2023-08-28T15:03:26.930887Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "i = 0\n", "x = 1\n", "i = 1\n", "x = 2\n", "i = 2\n", "x = 4\n", "i = 3\n", "x = 7\n", "i = 4\n", "x = 11\n", "That's it!\n" ] } ], "source": [ "# Look how indentation is used in python to define code blocks\n", "# Also note that range(5) produces numbers from 0 to 4\n", "\n", "x = 1\n", "for i in range(5):\n", " x = x + i\n", " print(\"i = \", i)\n", " print(\"x = \", x)\n", "\n", "print(\"That's it!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If statements\n", "-------------" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2023-08-28T15:03:26.932536Z", "iopub.status.busy": "2023-08-28T15:03:26.932436Z", "iopub.status.idle": "2023-08-28T15:03:26.934427Z", "shell.execute_reply": "2023-08-28T15:03:26.934188Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "i is different\n", "i is different\n", "i is 4\n", "i is 6\n", "i is different\n" ] } ], "source": [ "# Comparing symbols are == (equal), != (not equal), >, <, <=, >=, etc.\n", "\n", "for i in range(0, 10, 2):\n", " if i == 4:\n", " print(\"i is 4\")\n", " elif i == 6:\n", " print(\"i is 6\")\n", " else:\n", " print(\"i is different\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Defining a function\n", "-------------------" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2023-08-28T15:03:26.935841Z", "iopub.status.busy": "2023-08-28T15:03:26.935755Z", "iopub.status.idle": "2023-08-28T15:03:26.937522Z", "shell.execute_reply": "2023-08-28T15:03:26.937287Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.0\n" ] } ], "source": [ "# Define a new function\n", "def fnct(x):\n", " y = x**2 - 5.0\n", " return y\n", "\n", "print(fnct(3.))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Importing modules\n", "-----------------" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2023-08-28T15:03:26.938860Z", "iopub.status.busy": "2023-08-28T15:03:26.938769Z", "iopub.status.idle": "2023-08-28T15:03:26.940627Z", "shell.execute_reply": "2023-08-28T15:03:26.940422Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cos(pi/2) = 0.000\n", "cos(pi) = -1.000\n" ] } ], "source": [ "# In order to have access to new functions, you\n", "# import them from a library. Here we import mathematical functions\n", "# from the math library. After the import, python knows about\n", "# cos and pi\n", "\n", "# to import everything in module math: \n", "# from math import *\n", "from math import cos,pi\n", "\n", "print(\"cos(pi/2) = %.3f\"%(cos(pi/2.0)))\n", "print(\"cos(pi) = %.3f\"%(cos(pi)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists\n", "-----" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2023-08-28T15:03:26.941971Z", "iopub.status.busy": "2023-08-28T15:03:26.941880Z", "iopub.status.idle": "2023-08-28T15:03:26.943643Z", "shell.execute_reply": "2023-08-28T15:03:26.943425Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The second element of l is 2\n", "l3 is [1, 2, 3, 4, 5, 6]\n" ] } ], "source": [ "# Lists are defined with []\n", "# Note that indices start at 0 (not 1 like in Fortran)\n", "\n", "l = [1,2,3,4]\n", "print(\"The second element of l is \", l[1])\n", "\n", "# Lists are not vectors, adding lists appends them\n", "\n", "l2 = [5,6]\n", "l3 = l+l2\n", "\n", "print(\"l3 is \", l3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The numpy library\n", "-----------------\n", "\n", "[numpy](http://docs.scipy.org/doc/numpy/reference/) is a very important library in python. It mainly allows to manipulate\n", "arrays (matrices and vectors) and do linear algebra with them." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2023-08-28T15:03:26.944886Z", "iopub.status.busy": "2023-08-28T15:03:26.944814Z", "iopub.status.idle": "2023-08-28T15:03:26.982548Z", "shell.execute_reply": "2023-08-28T15:03:26.982320Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Adding term by term: [2 2 3 4]\n", "dot product: 7\n", "A x B = \n", "[[1 2]\n", " [3 4]]\n" ] } ], "source": [ "from numpy import array,dot\n", "\n", "# vectors\n", "v = array([1,1,2,3])\n", "w = array([1,1,1,1])\n", "print(\"Adding term by term: \", v+w)\n", "print(\"dot product: \", dot(v,w))\n", "\n", "# matrices\n", "A = array([[1,0],[0,1]])\n", "B = array([[1,2],[3,4]])\n", "\n", "print(\"A x B = \")\n", "print(dot(A,B))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Defining a new class\n", "--------------------\n", "\n", "This is for those that already know about object-oriented languages. In python, just like C++ for example, you can define your own classes. Here's an example:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2023-08-28T15:03:26.983897Z", "iopub.status.busy": "2023-08-28T15:03:26.983811Z", "iopub.status.idle": "2023-08-28T15:03:26.985930Z", "shell.execute_reply": "2023-08-28T15:03:26.985723Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x is 10\n", "x is 12\n" ] } ], "source": [ "# A new class\n", "# Note that all member functions must have \"self\" as a first argument\n", "class MyObject:\n", " \n", " # The constructor is called __init__\n", " def __init__(self, x):\n", " self.x = x\n", " \n", " def what_is_x(self):\n", " print(\"x is \", self.x)\n", " \n", " def change_x(self, x):\n", " self.x = x\n", " \n", "A = MyObject(10)\n", "A.what_is_x()\n", "A.change_x(12)\n", "A.what_is_x()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Getting help\n", "------------\n", "\n", "When you put a question mark after a command and type Ctrl-Enter it gives the help. If you type\n", "the parenthesis and then press Tab it will tell you what arguments are expected." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2023-08-28T15:03:26.987090Z", "iopub.status.busy": "2023-08-28T15:03:26.987024Z", "iopub.status.idle": "2023-08-28T15:03:26.988786Z", "shell.execute_reply": "2023-08-28T15:03:26.988579Z" } }, "outputs": [], "source": [ "array?" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "widgets": { "state": {}, "version": "1.1.1" } }, "nbformat": 4, "nbformat_minor": 4 }