{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Introduction to python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the first part of a python introduction and overview over interesting modules, by the PhD students of Frank Jülichers group at the Max Planck Institute for the Physics of Complex Systems, Dresden, Germany.\n", "This part was created by Rabea Seyboldt, April 2016. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world\n" ] } ], "source": [ "# my favorite program\n", "print 'Hello world'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Main message: python is simple (syntax, usage...)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "## Preliminaries" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- full programming language\n", "- script-based (no compiling, but slower)\n", "- high-level, many simple solutions\n", "- dynamic development (many changes, new addons)\n", "\n", "- here: concentrate on basic syntax, specialties, not features" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Modular, many packages\n", "- Internet (server/client)\n", "- Applets (graphical interface, buttons, etc)\n", "- Data management (pandas)\n", "- Bash-Commands (sys, os)\n", "- Making code faster (cython, numba)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Modular, many packages\n", "- Scientific packages (often Fortran/C-Code with python interface)\n", " - numpy/scipy (Matlab-like functions, arrays, numerical calculation)\n", " - sympy (Mathematica-like, symbolic calculation, not as advanced)\n", " - matplotlib (and seaborn, mayavi) (plotting)\n", " - scikit (machine learning, image analysis)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Installation\n", "- python (basics)\n", "- single modules\n", "- full package (scientific python)\n", "\n", "via\n", "- apt-get \n", "- package for mac/windows -> anaconda, canopy, python(x,y)\n", "- pip (python package installer) (uses pypi, the python packaging index)\n", "- install separate environments: virtualenv/virtualenvwrapper" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "### Usage\n", "- Use ipython/python commandline\n", " - use as calculator\n", " - import new module, test\n", "- write script name.py, \n", " - execute with python name.py\n", " - import as module in other script\n", "- use jupyter notebook\n", " - mixes html text incl. latex with code\n", " - similar to Mathematica notebooks" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Main syntax properties" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Minimal properties only...\n", "\n", "Googleing problems almost always helps" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Python 2 vs 3" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "0.333333333333\n" ] }, { "data": { "text/plain": [ "0.0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Beware: Division of integers in python 2\n", "i = 3\n", "print 1/i\n", "print 1./i\n", "1.//3." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.333333333333\n" ] } ], "source": [ "# The fix:\n", "from __future__ import division\n", "print 1/i\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or: Use python 3 (update of all module: ongoing), \n", "Syntax-Differenz: print('Hello world')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Function" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true, "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "def func1():\n", " # need to implement this\n", " # no brackets, indentation used for structuring\n", " # 'pass' needed: error otherwise\n", " pass " ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "True\n", "2.0\n", "[1, 2]\n", "{'key': 2}\n" ] } ], "source": [ "def func2(x):\n", " print x\n", "\n", "# multiple use\n", "func2('a') # string\n", "func2(True) # boolean\n", "func2(2.) # float\n", "func2([1,2]) # list\n", "func2({'key': 2}) # dictionary" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### If/for" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a smaller b\n", "a larger b\n", "a equals b\n" ] } ], "source": [ "def larger_or_smaller(a,b):\n", " if a == b:\n", " print 'a equals b'\n", " elif a < b:\n", " print 'a smaller b'\n", " else: print 'a larger b'\n", "\n", "larger_or_smaller(1,2) \n", "# works for int and float mixed\n", "larger_or_smaller(1,-1.)\n", "larger_or_smaller(1,1.)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "3\n", "4\n" ] } ], "source": [ "# range([start=0], stop, [step=1]) is an iterator\n", "# includes start, exclude stop\n", "for i in range(5):\n", " if i == 2: continue\n", " print i\n", " #if i >= 3: break" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Use bash and strings" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "256" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# import a module\n", "import os\n", "# execute a bash command (make directory)\n", "os.system('mkdir new_folder')\n", "\n", "# Using variables:\n", "# define integer variable\n", "n = 1 \n", "# define string (include integer)\n", "# other options: %s (string), %g (float notation)\n", "name = 'new_folder_%i' %n \n", "# make directory (and add strings)\n", "os.system('mkdir ' + name)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Very useful for passing parameters" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# define a dictionary\n", "params = {'parameter_a': 1, 'doSomething': True, 'mylist':[0,1]}\n", "# or\n", "params2 = dict(kw1=1, kw2=2)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'kw1': -1, 'parameter_a': 1, 'kw2': 2, 'mylist': [0, 2], 'doSomething': True}\n" ] } ], "source": [ "# change dictionary\n", "params.update({'mylist': [0,2]})\n", "params.update(params2)\n", "params['kw1'] = -1\n", "print params" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Passing parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use dictionarys for passing many (optional) parameters.\n", "\n", "Similar options to read from command line (python name.py param1 param2)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "kw1 : -1\n", "parameter_a : 1\n", "kw2 : 2\n", "mylist : [0, 2]\n", "doSomething : True\n", "0\n" ] } ], "source": [ "# passing unspecified parameters\n", "def func(*args, **kwargs):\n", " \n", " res = 0\n", " # iterate over args:\n", " for i in args:\n", " res *= i\n", " \n", " # iterate over kwargs\n", " for key in kwargs:\n", " print key, ': ', kwargs[key]\n", " return res\n", "\n", "print func(1, 2, **params)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "2\n" ] } ], "source": [ "# passing optional parameters\n", "def func(a=1, b=2, multiply=False):\n", " if multiply:\n", " return a*b\n", " else: return a+b\n", "print func()\n", "print func(multiply=True)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n" ] } ], "source": [ "# many parameters: use a dictionary\n", "params = dict(a=43, multiply=False, b=-1)\n", "print func(**params)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Main method" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "source": [ "''' This is a docstring, and it can be made into html doc of your package '''\n", "\n", "# this is a function\n", "def main():\n", " # indentation is what brackets are for other languages\n", " print 'Hello, world!'\n", "\n", "if __name__ == '__main__':\n", " # this is executed if the file is used via 'python name.py'\n", " main()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Errors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "try/except: check for wrong input etc" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "error!\n" ] }, { "data": { "text/plain": [ "0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def func(x):\n", " res = 0\n", " try: \n", " res = x-1\n", " except TypeError:\n", " print 'error!'\n", " return res\n", "func('e')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Assert: find errors in the code (for testing)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "ename": "AssertionError", "evalue": "x is not an integer: 'e'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mres\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'e'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mfunc\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mtypes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mIntType\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"x is not an integer: %r\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mres\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAssertionError\u001b[0m: x is not an integer: 'e'" ] } ], "source": [ "import types\n", "\n", "def func(x):\n", " assert type(x) is types.IntType, \"x is not an integer: %r\" % x\n", " res = x-1\n", " return res\n", "func('e')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Numpy arrays" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "- many functions specific for arrays:\n", " - transpose, eigenvalue\n", " - change of array-form (stacking, slices)\n", " - Minimum, specific values\n", "- most functions work for arrays (also try @vectorize)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(50, 50) 50\n" ] } ], "source": [ "import numpy as np\n", "\n", "# array with values from 0 to 10 (inclusive) with 50 points\n", "x = np.linspace(0,10, num=50, endpoint=True)\n", "# log-spacing\n", "y = np.logspace(-1,1)\n", "# 'init' array\n", "z = np.zeros_like(x)\n", "# 2d-arrays (for 3d plots etc)\n", "X, Y = np.meshgrid(x,y)\n", "# find the shape (all dimensions) or length (1st dimension, not only for arrays)\n", "print X.shape, len(X)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false, "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.0 10.0\n", "[ 0.20408163 0.6122449 1.02040816 1.42857143]\n" ] } ], "source": [ "# print first element, last element\n", "print x[0], x[-1]\n", "# print slice: x[min_index : max_index_exclusive : interval]\n", "print x[1:9:2]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Save data in files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- numpy savetxt/loadtxt (for arrays etc, human readable)\n", "- pickle (save/load python objects)\n", "- json (save/load dictionaries etc, human readable)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "x = np.linspace(0,10)\n", "np.savetxt('test_array.dat', x)\n", "y = np.loadtxt('test_array.dat')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Most important packages for everyday use: \n", "- numpy (arrays)\n", "- scipy (numerical things)\n", "- (scipy) (symbolic calculation)\n", "- matplotlib (plotting)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 x_{1} + \\cos{\\left (x_{1} \\right )}\n", "cos(1) + 2\n" ] } ], "source": [ "# import symbolic module\n", "import sympy as sy\n", "# define a variable, and the representation\n", "x, f = sy.symbols('x_1 f')\n", "\n", "# define an expression (sympy simple style)\n", "expr = x**2 + sy.sin(x)\n", "# symbolic derivative\n", "dexpr = sy.diff(expr,x)\n", "# print as latex\n", "print sy.latex(dexpr)\n", "f = sy.lambdify(x, dexpr, \"numpy\")\n", "print dexpr.subs(x,1)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Now let us calculate and plot values" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import sympy as sp\n", "\n", "y = np.linspace(0,10)\n", "z = f(y)\n", "\n", "%matplotlib qt\n", "import matplotlib as mpl\n", "import matplotlib.pyplot as plt\n", "\n", "plt.plot(y,z)\n", "plt.ylabel('Is this $x^2$?')\n", "plt.savefig('test.pdf')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "For more awesome stuff, more information, see:\n", "- next week (overview over many modules)\n", "- numpy/scipy documentation/tutorials\n", "- matplotlib gallery (Example) \n", "- many in-depth tutorials (Lecture)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }