{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This is part of [**Python for Geosciences**](https://github.com/koldunovn/python_for_geosciences) notes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "================" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Python](http://www.python.org/) is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages \n", "such as C. The language provides constructs intended to enable clear programs on both a small and large scale.\n", "\n", "-- From Wikipedia, the free encyclopedia" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python uses [duck typing](http://en.wikipedia.org/wiki/Duck_typing)" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Int" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 10" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "10" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "type(a)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "int" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Float" ] }, { "cell_type": "code", "collapsed": false, "input": [ "z = 10.\n", "z" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "10.0" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "type(z)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "float" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "String" ] }, { "cell_type": "code", "collapsed": false, "input": [ "b = '2'\n", "b" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "'2'" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some operations are not allowed on different types:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a+b" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'int' and 'str'", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "But some of them are allowed:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a*b" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "'2222222222'" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Might be a source of confusion :)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String variables can be combined:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c = ' guys walk into a bar'\n", "c" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "' guys walk into a bar'" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "b+c" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "'2 guys walk into a bar'" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to include variable of another type in to string you have to convert it:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str(a)+c" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "'10 guys walk into a bar'" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Everything is an object" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In IPython you can get the list of object's methods and attributes by typing dot and pressing TAB:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c." ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "' guys walk into a bar'" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Methods are basically default functions that can be applied to our variable:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c.upper()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "' GUYS WALK INTO A BAR'" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "c.title()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "' Guys Walk Into A Bar'" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "c.count('a')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "3" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "c.find('into')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "11" ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you need help on method in IPython type something like:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c.find?" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or open bracket and press TAB:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c.find(" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Int variable is also an object:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a.bit_length()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "4" ] } ], "prompt_number": 19 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Methods can be combined (kind of a pipeline)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c.title().count('a').bit_length()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "2" ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are several other interesting variable types in Python, but the one we would need the most is the list." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to create list put coma separated values in square brackets:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "l = [1,2,3,4,5]\n", "l" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "[1, 2, 3, 4, 5]" ] } ], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sort of similar to Matlab variables, but not exactly." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Values in list can be any type:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "l = ['one', 'two', 'three', 'four', 'five']\n", "l" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 24, "text": [ "['one', 'two', 'three', 'four', 'five']" ] } ], "prompt_number": 24 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Combined" ] }, { "cell_type": "code", "collapsed": false, "input": [ "l = ['one', 2, 'three', 4.0, 3+2]\n", "l" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 25, "text": [ "['one', 2, 'three', 4.0, 5]" ] } ], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any type means ANY type:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "l = ['one', 2, 'three', [1,2,3,4,5], 3+2]\n", "l" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "['one', 2, 'three', [1, 2, 3, 4, 5], 5]" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can access list values by index:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "l[0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 27, "text": [ "'one'" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Oh, yes, indexing starts with zero, so for Matlab users the zero is the new one :) See discussion on the matter [here](http://en.wikipedia.org/wiki/Zero-based_numbering)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "l[1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 28, "text": [ "2" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's have a look at the 4th element of our list:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "l[3]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 30, "text": [ "[1, 2, 3, 4, 5]" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's also a list, and its values can be accessed by indexes as well:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "l[3][4]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 31, "text": [ "5" ] } ], "prompt_number": 31 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You also can acces multiple elements of the list using slices:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "l[1:3]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 32, "text": [ "[2, 'three']" ] } ], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Slice will start with the first slice index and go up to but not including the second slice index. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "l[3]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "[1, 2, 3, 4, 5]" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Control Structures" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "For loop:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This loop will print all elements from the list *l*" ] }, { "cell_type": "code", "collapsed": false, "input": [ "l = ['one', 2, 'three', [1,2,3,4,5], 3+2]\n", "\n", "for element in l:\n", " print element" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "one\n", "2\n", "three\n", "[1, 2, 3, 4, 5]\n", "5\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two interesting thins here. First: indentation, it's in the code, you must use it, otherwise code will not work:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for element in l:\n", "print element" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "IndentationError", "evalue": "expected an indented block (, line 2)", "output_type": "pyerr", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m print element\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n" ] } ], "prompt_number": 34 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Second - you can iterate through the elements of the list. There is an option to iterate through a bunch of numbers as we used to in Matlab:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for index in range(5):\n", " print(l[index])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "one\n", "2\n", "three\n", "[1, 2, 3, 4, 5]\n", "5\n" ] } ], "prompt_number": 36 }, { "cell_type": "markdown", "metadata": {}, "source": [ "where *range* is just generating a list with sequence of numbers:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "range(5)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 35, "text": [ "[0, 1, 2, 3, 4]" ] } ], "prompt_number": 35 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Branches" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are not going to use branches in this notes, but this is how they look like just as another example of indentation use:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = -1\n", "if x > 0:\n", " print \"Melting\"\n", "elif x == 0:\n", " print \"Zero\"\n", "else:\n", " print \"Freezing\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Freezing\n" ] } ], "prompt_number": 37 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Modules" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pure python does not do much. Even IPython with *pylab* enabled can do only limited number of things. To do some specific tasks you need to import modules. Here I am going to demonstrate several ways to do so." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The most common one is to import complete library. In this example we import *urllib2* - a library for opening URLs using a variety of protocols." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import urllib2" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we get information from some ftp site. Note how function *urlopen* is called. We have to use name of the library, then dot, then name of the function from the library:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "response = urllib2.urlopen('ftp://ftp.zmaw.de/')\n", "html = response.read()\n", "html.splitlines()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "['dr-xr-x--- 17 ftp ftp 20480 Oct 26 03:15 incoming',\n", " 'dr-xr-x--- 51 ftp ftp 12288 Oct 26 03:30 outgoing']" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another option is to import it like this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from urllib2 import *" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case all functions will be imported in to the name-space and you can use *urlopen* directly, without typing the name of the library first:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "response = urlopen('ftp://ftp.zmaw.de/')\n", "html = response.read()\n", "html.splitlines()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "['dr-xr-x--- 17 ftp ftp 20480 Oct 26 03:15 incoming',\n", " 'dr-xr-x--- 51 ftp ftp 12288 Oct 26 03:30 outgoing']" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "But generally I think it's a bad idea, because your name-space is populated by things that you don't really need and it's hard to tell where the function comes from." ] }, { "cell_type": "code", "collapsed": true, "input": [ "whos" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Variable Type Data/Info\n", "-------------------------------------------------------------------------\n", "AbstractBasicAuthHandler classobj urllib2.AbstractBasicAuthHandler\n", "AbstractDigestAuthHandler classobj urllib2.AbstractDigestAuthHandler\n", "AbstractHTTPHandler classobj urllib2.AbstractHTTPHandler\n", "BaseHandler classobj urllib2.BaseHandler\n", "CacheFTPHandler classobj urllib2.CacheFTPHandler\n", "FTPHandler classobj urllib2.FTPHandler\n", "FileHandler classobj urllib2.FileHandler\n", "HTTPBasicAuthHandler classobj urllib2.HTTPBasicAuthHandler\n", "HTTPCookieProcessor classobj urllib2.HTTPCookieProcessor\n", "HTTPDefaultErrorHandler classobj urllib2.HTTPDefaultErrorHandler\n", "HTTPDigestAuthHandler classobj urllib2.HTTPDigestAuthHandler\n", "HTTPError type \n", "HTTPErrorProcessor classobj urllib2.HTTPErrorProcessor\n", "HTTPHandler classobj urllib2.HTTPHandler\n", "HTTPPasswordMgr classobj urllib2.HTTPPasswordMgr\n", "HTTPPasswordMgrWithDefaultRealm classobj urllib2.HTTPPasswordMgrWithDefaultRealm\n", "HTTPRedirectHandler classobj urllib2.HTTPRedirectHandler\n", "HTTPSHandler classobj urllib2.HTTPSHandler\n", "OpenerDirector classobj urllib2.OpenerDirector\n", "ProxyBasicAuthHandler classobj urllib2.ProxyBasicAuthHandler\n", "ProxyDigestAuthHandler classobj urllib2.ProxyDigestAuthHandler\n", "ProxyHandler classobj urllib2.ProxyHandler\n", "Request classobj urllib2.Request\n", "StringIO builtin_function_or_method \n", "URLError type \n", "UnknownHandler classobj urllib2.UnknownHandler\n", "a int 10\n", "addinfourl classobj urllib.addinfourl\n", "b str 2\n", "base64 module ib/python2.7/base64.pyc'>\n", "bisect module ib/python2.7/bisect.pyc'>\n", "build_opener function \n", "c str guys walk into a bar\n", "element int 5\n", "ftpwrapper classobj urllib.ftpwrapper\n", "getproxies function \n", "hashlib module b/python2.7/hashlib.pyc'>\n", "html str dr-xr-x--- 16 ftp f<...> Oct 23 03:30 outgoing\r", "\\n\n", "httplib module b/python2.7/httplib.pyc'>\n", "index int 4\n", "install_opener function \n", "l list n=5\n", "localhost function \n", "mimetools module python2.7/mimetools.pyc'>\n", "os module \n", "parse_http_list function \n", "parse_keqv_list function \n", "posixpath module python2.7/posixpath.pyc'>\n", "proxy_bypass function \n", "quote function \n", "randombytes function \n", "re module \n", "request_host function \n", "response urllib.addinfourl ct object at 0x33af350>>>\n", "socket module ib/python2.7/socket.pyc'>\n", "splitattr function \n", "splithost function \n", "splitpasswd function \n", "splitport function \n", "splittag function \n", "splittype function \n", "splituser function \n", "splitvalue function \n", "time module \n", "unquote function \n", "url2pathname function \n", "urllib2 module b/python2.7/urllib2.pyc'>\n", "urlopen function \n", "urlparse module /python2.7/urlparse.pyc'>\n", "x int -1\n", "z float 10.0\n" ] } ], "prompt_number": 43 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can import only function that you need:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from urllib2 import urlopen" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "response = urlopen('ftp://ftp.zmaw.de/')\n", "html = response.read()\n", "html.splitlines()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 44, "text": [ "['dr-xr-x--- 16 ftp ftp 20480 Oct 23 15:51 incoming',\n", " 'dr-xr-x--- 49 ftp ftp 12288 Oct 23 03:30 outgoing']" ] } ], "prompt_number": 44 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or import library as alias in order to avoid extensive typing:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import urllib2 as ul" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "response = ul.urlopen('ftp://ftp.zmaw.de/')\n", "html = response.read()\n", "html.splitlines()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "['dr-xr-x--- 17 ftp ftp 20480 Oct 26 03:15 incoming',\n", " 'dr-xr-x--- 51 ftp ftp 12288 Oct 26 03:30 outgoing']" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Links:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Dive Into Python](http://www.diveintopython.net/index.html)" ] } ], "metadata": {} } ] }