{ "metadata": { "name": "", "signature": "sha256:39682d19661e944819b18f364643dd1690df711499d18b875e561dd4b9d00b48" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "from __future__ import print_function" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Interactive Python" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"hello\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "hello\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "4 + 5" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "9" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "3.5 + 'e'" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'float' and 'str'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m3.5\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'e'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'float' and 'str'" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Built-in data types" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Lists" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [1,2,3,4,5]\n", "a[0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "1" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "a[0] + a[1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "3" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "range(5)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "[0, 1, 2, 3, 4]" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Iterating over lists" ] }, { "cell_type": "code", "collapsed": false, "input": [ "range(5) #result is a list" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "[0, 1, 2, 3, 4]" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "a = [0, 1]\n", "for i in range(20):\n", " a += [ a[-1] + a[-2] ]\n", "\n", "print(a) " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946]\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "List comprehensions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "[a[i+1]/a[i+2] for i in range(20)]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "What?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from __future__ import division" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "[a[i+1]/a[i+2] for i in range(20)]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "[1.0,\n", " 0.5,\n", " 0.6666666666666666,\n", " 0.6,\n", " 0.625,\n", " 0.6153846153846154,\n", " 0.6190476190476191,\n", " 0.6176470588235294,\n", " 0.6181818181818182,\n", " 0.6179775280898876,\n", " 0.6180555555555556,\n", " 0.6180257510729614,\n", " 0.6180371352785146,\n", " 0.6180327868852459,\n", " 0.6180344478216818,\n", " 0.6180338134001252,\n", " 0.6180340557275542,\n", " 0.6180339631667066,\n", " 0.6180339985218034,\n", " 0.618033985017358]" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "[a[i+1]/float(a[i+2]) for i in range(20)]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "[1.0,\n", " 0.5,\n", " 0.6666666666666666,\n", " 0.6,\n", " 0.625,\n", " 0.6153846153846154,\n", " 0.6190476190476191,\n", " 0.6176470588235294,\n", " 0.6181818181818182,\n", " 0.6179775280898876,\n", " 0.6180555555555556,\n", " 0.6180257510729614,\n", " 0.6180371352785146,\n", " 0.6180327868852459,\n", " 0.6180344478216818,\n", " 0.6180338134001252,\n", " 0.6180340557275542,\n", " 0.6180339631667066,\n", " 0.6180339985218034,\n", " 0.618033985017358]" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Other list functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "0 in a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "True" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "0.5 in a" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "b = [a, 2, 3] * 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "b" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a[0] = 1\n", "a" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "b" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "len(a)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "max(a), min(a)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(a), type(a[0])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "List object functions" ] }, { "cell_type": "code", "collapsed": true, "input": [ "dir(a)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a.append(3)\n", "a[-1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 20, "text": [ "3" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "def func(e):\n", " return e\n", "\n", "a.append(func)\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": [ "[0,\n", " 1,\n", " 1,\n", " 2,\n", " 3,\n", " 5,\n", " 8,\n", " 13,\n", " 21,\n", " 34,\n", " 55,\n", " 89,\n", " 144,\n", " 233,\n", " 377,\n", " 610,\n", " 987,\n", " 1597,\n", " 2584,\n", " 4181,\n", " 6765,\n", " 10946,\n", " 3,\n", " ]" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "a[-1](10), a[-1]('hello')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "(10, 'hello')" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "a.count(1)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "2" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "a.reverse()\n", "a[2:]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 24, "text": [ "[10946,\n", " 6765,\n", " 4181,\n", " 2584,\n", " 1597,\n", " 987,\n", " 610,\n", " 377,\n", " 233,\n", " 144,\n", " 89,\n", " 55,\n", " 34,\n", " 21,\n", " 13,\n", " 8,\n", " 5,\n", " 3,\n", " 2,\n", " 1,\n", " 1,\n", " 0]" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "a.sort()\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 25, "text": [ "[0,\n", " 1,\n", " 1,\n", " 2,\n", " 3,\n", " 3,\n", " 5,\n", " 8,\n", " 13,\n", " 21,\n", " 34,\n", " 55,\n", " 89,\n", " 144,\n", " 233,\n", " 377,\n", " 610,\n", " 987,\n", " 1597,\n", " 2584,\n", " 4181,\n", " 6765,\n", " 10946,\n", " ]" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "a.index(3)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 26, "text": [ "4" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "a.count(3)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 27, "text": [ "2" ] } ], "prompt_number": 27 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Slicing" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a[2:-5]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "[1, 2, 3, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "a[:-5]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 38, "text": [ "[0, 1, 1, 2, 3, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]" ] } ], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "a[2::2] # Third number in slice is element jump (useful for deinterleaving!)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 34, "text": [ "[1, 3, 5, 13, 34, 89, 233, 610, 1597, 4181, 10946]" ] } ], "prompt_number": 34 }, { "cell_type": "markdown", "metadata": {}, "source": [ "More: http://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange\n" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Strings" ] }, { "cell_type": "code", "collapsed": false, "input": [ "name = 'andres'\n", "surname = \"cabrera\"\n", "name + ' ' + surname" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 39, "text": [ "'andres cabrera'" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "_.capitalize() # ipython history!" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 40, "text": [ "'Andres cabrera'" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "__.title()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 41, "text": [ "'Andres Cabrera'" ] } ], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "___.upper()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 42, "text": [ "'ANDRES CABRERA'" ] } ], "prompt_number": 42 }, { "cell_type": "code", "collapsed": false, "input": [ "fullname = __" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 43 }, { "cell_type": "code", "collapsed": false, "input": [ "fullname.count('A'), fullname.isalpha(), fullname.startswith('A')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 44, "text": [ "(1, False, True)" ] } ], "prompt_number": 44 }, { "cell_type": "code", "collapsed": false, "input": [ "fullname.title().split()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 45, "text": [ "['Andres', 'Cabrera']" ] } ], "prompt_number": 45 }, { "cell_type": "code", "collapsed": false, "input": [ "' '.join([name, surname])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 46, "text": [ "'andres cabrera'" ] } ], "prompt_number": 46 }, { "cell_type": "code", "collapsed": false, "input": [ "fullname.replace('A', 'a')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 47, "text": [ "'andres Cabrera'" ] } ], "prompt_number": 47 }, { "cell_type": "code", "collapsed": false, "input": [ "fullname" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 48, "text": [ "'Andres Cabrera'" ] } ], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "fullname = (\"%s %s\"%(name, surname)).title()\n", "print(fullname)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Andres Cabrera\n" ] } ], "prompt_number": 50 }, { "cell_type": "code", "collapsed": false, "input": [ "'90'.zfill(4)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 51, "text": [ "'0090'" ] } ], "prompt_number": 51 }, { "cell_type": "code", "collapsed": false, "input": [ "'90'.center(4)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 52, "text": [ "' 90 '" ] } ], "prompt_number": 52 }, { "cell_type": "markdown", "metadata": {}, "source": [ "More info: http://docs.python.org/2/library/stdtypes.html#string-methods\n" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Boolean" ] }, { "cell_type": "code", "collapsed": false, "input": [ "(1 + 1) == 3" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 53, "text": [ "False" ] } ], "prompt_number": 53 }, { "cell_type": "code", "collapsed": false, "input": [ "e = 3 == 4" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 54 }, { "cell_type": "code", "collapsed": false, "input": [ "not e" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 55, "text": [ "True" ] } ], "prompt_number": 55 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Dictionaries" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d = {'name': 'andres', 'surname': 'cabrera'}\n", "fullname = \"%s %s\"%(d['name'], d['surname'])\n", "fullname.title()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 56, "text": [ "'Andres Cabrera'" ] } ], "prompt_number": 56 }, { "cell_type": "code", "collapsed": false, "input": [ "d = { 1: 'hello', func : 'world'}" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 59 }, { "cell_type": "code", "collapsed": false, "input": [ "d[1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 58, "text": [ "'hello'" ] } ], "prompt_number": 58 }, { "cell_type": "code", "collapsed": false, "input": [ "d[func]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 60, "text": [ "'world'" ] } ], "prompt_number": 60 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Indentation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Scope is defined by indentation, not special characters!" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def myadd(first, second):\n", " return first + second" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 61 }, { "cell_type": "code", "collapsed": false, "input": [ "myadd(3,4)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 62, "text": [ "7" ] } ], "prompt_number": 62 }, { "cell_type": "code", "collapsed": false, "input": [ "myadd('a', 'b')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 63, "text": [ "'ab'" ] } ], "prompt_number": 63 }, { "cell_type": "code", "collapsed": false, "input": [ "myadd(3, 'a')" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'int' and 'str'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmyadd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'a'\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;36mmyadd\u001b[0;34m(first, second)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmyadd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfirst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msecond\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfirst\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0msecond\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" ] } ], "prompt_number": 64 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Iteration and loops" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [0, 1]\n", "for i in range(20):\n", " a += [ a[-1] + a[-2] ]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 65 }, { "cell_type": "code", "collapsed": false, "input": [ "a = [0, 1]\n", "while a[-1] < 1000:\n", " a.append(a[-1] + a[-2])\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 66, "text": [ "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]" ] } ], "prompt_number": 66 }, { "cell_type": "code", "collapsed": false, "input": [ "d = {'name': 'andres', 'surname': 'cabrera'}\n", "for key in d:\n", " print(\"%s:%s\"%(key, d[key]))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "surname:cabrera\n", "name:andres\n" ] } ], "prompt_number": 70 }, { "cell_type": "code", "collapsed": false, "input": [ "for char in name:\n", " # print char, #v.2 style\n", " print(char)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a\n", "n\n", "d\n", "r\n", "e\n", "s\n" ] } ], "prompt_number": 72 }, { "cell_type": "code", "collapsed": false, "input": [ "for char in name:\n", " # print char, #v.2 style\n", " print(char, end='')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "andres" ] } ], "prompt_number": 73 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Help in Ipython" ] }, { "cell_type": "code", "collapsed": false, "input": [ "str?" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 74 }, { "cell_type": "code", "collapsed": false, "input": [ "name.center?" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 75 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or use the shift+tab key to display pop-up help (in notebook only)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "help(str)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Help on class str in module __builtin__:\n", "\n", "class str(basestring)\n", " | str(object='') -> string\n", " | \n", " | Return a nice string representation of the object.\n", " | If the argument is a string, the return value is the same object.\n", " | \n", " | Method resolution order:\n", " | str\n", " | basestring\n", " | object\n", " | \n", " | Methods defined here:\n", " | \n", " | __add__(...)\n", " | x.__add__(y) <==> x+y\n", " | \n", " | __contains__(...)\n", " | x.__contains__(y) <==> y in x\n", " | \n", " | __eq__(...)\n", " | x.__eq__(y) <==> x==y\n", " | \n", " | __format__(...)\n", " | S.__format__(format_spec) -> string\n", " | \n", " | Return a formatted version of S as described by format_spec.\n", " | \n", " | __ge__(...)\n", " | x.__ge__(y) <==> x>=y\n", " | \n", " | __getattribute__(...)\n", " | x.__getattribute__('name') <==> x.name\n", " | \n", " | __getitem__(...)\n", " | x.__getitem__(y) <==> x[y]\n", " | \n", " | __getnewargs__(...)\n", " | \n", " | __getslice__(...)\n", " | x.__getslice__(i, j) <==> x[i:j]\n", " | \n", " | Use of negative indices is not supported.\n", " | \n", " | __gt__(...)\n", " | x.__gt__(y) <==> x>y\n", " | \n", " | __hash__(...)\n", " | x.__hash__() <==> hash(x)\n", " | \n", " | __le__(...)\n", " | x.__le__(y) <==> x<=y\n", " | \n", " | __len__(...)\n", " | x.__len__() <==> len(x)\n", " | \n", " | __lt__(...)\n", " | x.__lt__(y) <==> x x%y\n", " | \n", " | __mul__(...)\n", " | x.__mul__(n) <==> x*n\n", " | \n", " | __ne__(...)\n", " | x.__ne__(y) <==> x!=y\n", " | \n", " | __repr__(...)\n", " | x.__repr__() <==> repr(x)\n", " | \n", " | __rmod__(...)\n", " | x.__rmod__(y) <==> y%x\n", " | \n", " | __rmul__(...)\n", " | x.__rmul__(n) <==> n*x\n", " | \n", " | __sizeof__(...)\n", " | S.__sizeof__() -> size of S in memory, in bytes\n", " | \n", " | __str__(...)\n", " | x.__str__() <==> str(x)\n", " | \n", " | capitalize(...)\n", " | S.capitalize() -> string\n", " | \n", " | Return a copy of the string S with only its first character\n", " | capitalized.\n", " | \n", " | center(...)\n", " | S.center(width[, fillchar]) -> string\n", " | \n", " | Return S centered in a string of length width. Padding is\n", " | done using the specified fill character (default is a space)\n", " | \n", " | count(...)\n", " | S.count(sub[, start[, end]]) -> int\n", " | \n", " | Return the number of non-overlapping occurrences of substring sub in\n", " | string S[start:end]. Optional arguments start and end are interpreted\n", " | as in slice notation.\n", " | \n", " | decode(...)\n", " | S.decode([encoding[,errors]]) -> object\n", " | \n", " | Decodes S using the codec registered for encoding. encoding defaults\n", " | to the default encoding. errors may be given to set a different error\n", " | handling scheme. Default is 'strict' meaning that encoding errors raise\n", " | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n", " | as well as any other name registered with codecs.register_error that is\n", " | able to handle UnicodeDecodeErrors.\n", " | \n", " | encode(...)\n", " | S.encode([encoding[,errors]]) -> object\n", " | \n", " | Encodes S using the codec registered for encoding. encoding defaults\n", " | to the default encoding. errors may be given to set a different error\n", " | handling scheme. Default is 'strict' meaning that encoding errors raise\n", " | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n", " | 'xmlcharrefreplace' as well as any other name registered with\n", " | codecs.register_error that is able to handle UnicodeEncodeErrors.\n", " | \n", " | endswith(...)\n", " | S.endswith(suffix[, start[, end]]) -> bool\n", " | \n", " | Return True if S ends with the specified suffix, False otherwise.\n", " | With optional start, test S beginning at that position.\n", " | With optional end, stop comparing S at that position.\n", " | suffix can also be a tuple of strings to try.\n", " | \n", " | expandtabs(...)\n", " | S.expandtabs([tabsize]) -> string\n", " | \n", " | Return a copy of S where all tab characters are expanded using spaces.\n", " | If tabsize is not given, a tab size of 8 characters is assumed.\n", " | \n", " | find(...)\n", " | S.find(sub [,start [,end]]) -> int\n", " | \n", " | Return the lowest index in S where substring sub is found,\n", " | such that sub is contained within S[start:end]. Optional\n", " | arguments start and end are interpreted as in slice notation.\n", " | \n", " | Return -1 on failure.\n", " | \n", " | format(...)\n", " | S.format(*args, **kwargs) -> string\n", " | \n", " | Return a formatted version of S, using substitutions from args and kwargs.\n", " | The substitutions are identified by braces ('{' and '}').\n", " | \n", " | index(...)\n", " | S.index(sub [,start [,end]]) -> int\n", " | \n", " | Like S.find() but raise ValueError when the substring is not found.\n", " | \n", " | isalnum(...)\n", " | S.isalnum() -> bool\n", " | \n", " | Return True if all characters in S are alphanumeric\n", " | and there is at least one character in S, False otherwise.\n", " | \n", " | isalpha(...)\n", " | S.isalpha() -> bool\n", " | \n", " | Return True if all characters in S are alphabetic\n", " | and there is at least one character in S, False otherwise.\n", " | \n", " | isdigit(...)\n", " | S.isdigit() -> bool\n", " | \n", " | Return True if all characters in S are digits\n", " | and there is at least one character in S, False otherwise.\n", " | \n", " | islower(...)\n", " | S.islower() -> bool\n", " | \n", " | Return True if all cased characters in S are lowercase and there is\n", " | at least one cased character in S, False otherwise.\n", " | \n", " | isspace(...)\n", " | S.isspace() -> bool\n", " | \n", " | Return True if all characters in S are whitespace\n", " | and there is at least one character in S, False otherwise.\n", " | \n", " | istitle(...)\n", " | S.istitle() -> bool\n", " | \n", " | Return True if S is a titlecased string and there is at least one\n", " | character in S, i.e. uppercase characters may only follow uncased\n", " | characters and lowercase characters only cased ones. Return False\n", " | otherwise.\n", " | \n", " | isupper(...)\n", " | S.isupper() -> bool\n", " | \n", " | Return True if all cased characters in S are uppercase and there is\n", " | at least one cased character in S, False otherwise.\n", " | \n", " | join(...)\n", " | S.join(iterable) -> string\n", " | \n", " | Return a string which is the concatenation of the strings in the\n", " | iterable. The separator between elements is S.\n", " | \n", " | ljust(...)\n", " | S.ljust(width[, fillchar]) -> string\n", " | \n", " | Return S left-justified in a string of length width. Padding is\n", " | done using the specified fill character (default is a space).\n", " | \n", " | lower(...)\n", " | S.lower() -> string\n", " | \n", " | Return a copy of the string S converted to lowercase.\n", " | \n", " | lstrip(...)\n", " | S.lstrip([chars]) -> string or unicode\n", " | \n", " | Return a copy of the string S with leading whitespace removed.\n", " | If chars is given and not None, remove characters in chars instead.\n", " | If chars is unicode, S will be converted to unicode before stripping\n", " | \n", " | partition(...)\n", " | S.partition(sep) -> (head, sep, tail)\n", " | \n", " | Search for the separator sep in S, and return the part before it,\n", " | the separator itself, and the part after it. If the separator is not\n", " | found, return S and two empty strings.\n", " | \n", " | replace(...)\n", " | S.replace(old, new[, count]) -> string\n", " | \n", " | Return a copy of string S with all occurrences of substring\n", " | old replaced by new. If the optional argument count is\n", " | given, only the first count occurrences are replaced.\n", " | \n", " | rfind(...)\n", " | S.rfind(sub [,start [,end]]) -> int\n", " | \n", " | Return the highest index in S where substring sub is found,\n", " | such that sub is contained within S[start:end]. Optional\n", " | arguments start and end are interpreted as in slice notation.\n", " | \n", " | Return -1 on failure.\n", " | \n", " | rindex(...)\n", " | S.rindex(sub [,start [,end]]) -> int\n", " | \n", " | Like S.rfind() but raise ValueError when the substring is not found.\n", " | \n", " | rjust(...)\n", " | S.rjust(width[, fillchar]) -> string\n", " | \n", " | Return S right-justified in a string of length width. Padding is\n", " | done using the specified fill character (default is a space)\n", " | \n", " | rpartition(...)\n", " | S.rpartition(sep) -> (head, sep, tail)\n", " | \n", " | Search for the separator sep in S, starting at the end of S, and return\n", " | the part before it, the separator itself, and the part after it. If the\n", " | separator is not found, return two empty strings and S.\n", " | \n", " | rsplit(...)\n", " | S.rsplit([sep [,maxsplit]]) -> list of strings\n", " | \n", " | Return a list of the words in the string S, using sep as the\n", " | delimiter string, starting at the end of the string and working\n", " | to the front. If maxsplit is given, at most maxsplit splits are\n", " | done. If sep is not specified or is None, any whitespace string\n", " | is a separator.\n", " | \n", " | rstrip(...)\n", " | S.rstrip([chars]) -> string or unicode\n", " | \n", " | Return a copy of the string S with trailing whitespace removed.\n", " | If chars is given and not None, remove characters in chars instead.\n", " | If chars is unicode, S will be converted to unicode before stripping\n", " | \n", " | split(...)\n", " | S.split([sep [,maxsplit]]) -> list of strings\n", " | \n", " | Return a list of the words in the string S, using sep as the\n", " | delimiter string. If maxsplit is given, at most maxsplit\n", " | splits are done. If sep is not specified or is None, any\n", " | whitespace string is a separator and empty strings are removed\n", " | from the result.\n", " | \n", " | splitlines(...)\n", " | S.splitlines(keepends=False) -> list of strings\n", " | \n", " | Return a list of the lines in S, breaking at line boundaries.\n", " | Line breaks are not included in the resulting list unless keepends\n", " | is given and true.\n", " | \n", " | startswith(...)\n", " | S.startswith(prefix[, start[, end]]) -> bool\n", " | \n", " | Return True if S starts with the specified prefix, False otherwise.\n", " | With optional start, test S beginning at that position.\n", " | With optional end, stop comparing S at that position.\n", " | prefix can also be a tuple of strings to try.\n", " | \n", " | strip(...)\n", " | S.strip([chars]) -> string or unicode\n", " | \n", " | Return a copy of the string S with leading and trailing\n", " | whitespace removed.\n", " | If chars is given and not None, remove characters in chars instead.\n", " | If chars is unicode, S will be converted to unicode before stripping\n", " | \n", " | swapcase(...)\n", " | S.swapcase() -> string\n", " | \n", " | Return a copy of the string S with uppercase characters\n", " | converted to lowercase and vice versa.\n", " | \n", " | title(...)\n", " | S.title() -> string\n", " | \n", " | Return a titlecased version of S, i.e. words start with uppercase\n", " | characters, all remaining cased characters have lowercase.\n", " | \n", " | translate(...)\n", " | S.translate(table [,deletechars]) -> string\n", " | \n", " | Return a copy of the string S, where all characters occurring\n", " | in the optional argument deletechars are removed, and the\n", " | remaining characters have been mapped through the given\n", " | translation table, which must be a string of length 256 or None.\n", " | If the table argument is None, no translation is applied and\n", " | the operation simply removes the characters in deletechars.\n", " | \n", " | upper(...)\n", " | S.upper() -> string\n", " | \n", " | Return a copy of the string S converted to uppercase.\n", " | \n", " | zfill(...)\n", " | S.zfill(width) -> string\n", " | \n", " | Pad a numeric string S with zeros on the left, to fill a field\n", " | of the specified width. The string S is never truncated.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data and other attributes defined here:\n", " | \n", " | __new__ = \n", " | T.__new__(S, ...) -> a new object with type S, a subtype of T\n", "\n" ] } ], "prompt_number": 76 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Modules and namespaces" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import glob" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Python Standard Library: http://docs.python.org/3/library/\n", "\n", "Documentation for the glob module: http://docs.python.org/3/library/glob.html" ] }, { "cell_type": "code", "collapsed": false, "input": [ "glob.glob(\"*\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "glob.glob(\"/home/andres/*.css\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from random import random\n", "random()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from os import chdir" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "chdir(\"/home/andres/\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from os import *\n", "listdir('.')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Importing whole modules into the global namespace is not generally recommended although it can be very convenient" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Files" ] }, { "cell_type": "code", "collapsed": false, "input": [ "f = open('/home/andres/test.css', 'r')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "open" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "help(open)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "del open # yes! Importing a whole module to the global namespace can be bad!!" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "open" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "f = open('/home/andres/test.css', 'r')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "lines = f.readlines()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "lines[:10]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "f.close()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "f = open('/home/andres/test.txt','w')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "f.write(fullname)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "f.close()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "f = open('/home/andres/test.txt', 'r')\n", "f.read()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "More: http://docs.python.org/2/library/functions.html#open " ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Numpy arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you have run ipython with the *--pylab* option, many numpy and scipy modules and functions are already available.\n", "\n", "You can active this with the ipython \"magic\" command:\n", "\n", " %pylab\n", "\n", "Otherwise, on a regular Python shell, use:\n", "\n", " import numpy as np\n", " from pylab import *\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "linspace(0, 1, 10)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "linspace(0,1, 10, endpoint=False)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "ones(10)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "ones([2,2])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "arange(10)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also create arrays from a \"generator\" function:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def f(x,y):\n", " return 10 * x + y\n", "\n", "fromfunction(f, (5, 4), dtype= int)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Array operations" ] }, { "cell_type": "code", "collapsed": false, "input": [ "arr = fromfunction(f, (5, 4), dtype= int)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Scalar operations" ] }, { "cell_type": "code", "collapsed": false, "input": [ "arr * 2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Matrix operations" ] }, { "cell_type": "code", "collapsed": false, "input": [ "arr" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "arr * [3,4,5,6]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "dot(arr, [3,4,5,6])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "dot(arr, arr.T)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "c_[arr, [2,3,4,5,6]]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "r_[arr, arr*2]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "r_[arr, [2,3,4,5]]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python lists are interpreted as column vectors!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "np.array([2,3,4,5,6])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "r_[arr, np.array([2,3,4,5,6])]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And so are 1D arrays!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "np.array([2,3,4,5,6], ndmin = 2)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "np.array([2,3,4,5,6], ndmin = 2).T" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "r_[arr, np.array([2,3,4,5], ndmin = 2)]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But a 2D array with a single line is a row!" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Array properties" ] }, { "cell_type": "code", "collapsed": false, "input": [ "arr.shape" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "arr.ndim" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "arr.dtype" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "arr.size" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Functions on arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Too many to name here but for example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sin(arr)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "sum(arr)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "mean(arr), median(arr), arr.max(), arr.min()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Iterating" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for row in arr:\n", " print row" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for element in arr.flat:\n", " print element," ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "View or shallow copies" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c = arr.view()\n", "c" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "c is arr" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "c.base is arr" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "c.flags.owndata" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "c.shape = [2, 10]\n", "c" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "arr" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "c[0,4] = 99\n", "c, arr" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To make a deep (independent) copy:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "d = arr.copy()\n", "d[:] = 10\n", "d" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "arr" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Graphing" ] }, { "cell_type": "code", "collapsed": false, "input": [ "plot(a)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great tutorial: http://scipy-lectures.github.io/intro/language/python_language.html\n", "\n", "Another more in depth tutorial: http://nbviewer.ipython.org/gist/rpmuller/5920182" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By: Andr\u00e9s Cabrera mantaraya36@gmail.com\n", "\n", "For course MAT 240E at UCSB\n", "\n", "This ipython notebook is licensed under the CC-BY-NC-SA license: http://creativecommons.org/licenses/by-nc-sa/4.0/\n", "\n", "![http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png](http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png)" ] } ], "metadata": {} } ] }