{ "metadata": { "name": "Python_101" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Strings" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\"hello world\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 1, "text": [ "'hello world'" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "'hello again'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 2, "text": [ "'hello again'" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "\"\"\"A string\n", "that spans\n", "multiple\n", "lines\"\"\"\n", "# Note how python identifies `\\n` as the linebreak character" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 4, "text": [ "'A string\\nthat spans\\nmultiple\\nlines'" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "hw = \"hello world\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "print hw" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "hello world\n" ] } ], "prompt_number": 6 }, { "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]) -> 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": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "new_string = \"hello\" + \"world\" # Note no spaces within the \"s!" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "print new_string" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "helloworld\n" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "new_string = \"hello \" + \"world\" # Note the space" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "print new_string" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "hello world\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "print \"This is the first %s %s wrote\" % ( \"code\", \"Jose\" )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is the first code Jose wrote\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "what_wrote = \"code\"\n", "who_wrote = \"Jose\"\n", "what_year = 2012\n", "print \"This is the first %s %s wrote in %d\" % ( what_wrote, who_wrote, what_year )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is the first code Jose wrote in 2012\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "String methods" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings are **objects** and have **methods** associated with them. Methods are just a number of ways to do something with objects. For example, for a string, you might want to split it into substrings, find where one substring starts, or replace substrings with other substrings... \n", "Have a look at [string documentation](http://docs.python.org/library/stdtypes.html#string-methods)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "hw = \"hello world\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "hw.count ( \"l\" ) # Count the number of times \"l\" apears in hw" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 15, "text": [ "3" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "hw.find ( \"l\" ) # Find the first location of \"l\" in hw. Note that python starts counting at 0, not 1!" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 16, "text": [ "2" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "hw.find ( \"lo\" ) # Ditto" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 17, "text": [ "3" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "hw.isdigit () # Is this string a digit?" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 18, "text": [ "False" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "hw.replace ( \"world\", \"everyone\" ) # Replace one substring with another" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 19, "text": [ "'hello everyone'" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "hw.split () # Split the string into words" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 20, "text": [ "['hello', 'world']" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "line = raw_input ( \"Enter something: \" )\n", "line = line.strip()\n", "while True:\n", " if line == \"\" or line == \"q\":\n", " break\n", " try:\n", " print line\n", " except:\n", " pass\n", " line = raw_input ( \"Enter something: \" )\n", " line = line.strip()" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "StdinNotImplementedError", "evalue": "raw_input was called, but this frontend does not support stdin.", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mStdinNotImplementedError\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[0mline\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mraw_input\u001b[0m \u001b[1;33m(\u001b[0m \u001b[1;34m\"Enter something: \"\u001b[0m \u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mline\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mline\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstrip\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[0mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mline\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"\"\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mline\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"q\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m/usr/local/lib/python2.7/dist-packages/IPython/zmq/ipkernel.pyc\u001b[0m in \u001b[0;36m\u001b[1;34m(prompt)\u001b[0m\n\u001b[0;32m 343\u001b[0m \u001b[0mraw_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_raw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprompt\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mident\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 344\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 345\u001b[1;33m \u001b[0mraw_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m \u001b[1;33m:\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_no_raw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 346\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 347\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mpy3compat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPY3\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m/usr/local/lib/python2.7/dist-packages/IPython/zmq/ipkernel.pyc\u001b[0m in \u001b[0;36m_no_raw_input\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 686\u001b[0m \"\"\"Raise StdinNotImplentedError if active frontend doesn't support\n\u001b[0;32m 687\u001b[0m stdin.\"\"\"\n\u001b[1;32m--> 688\u001b[1;33m raise StdinNotImplementedError(\"raw_input was called, but this \"\n\u001b[0m\u001b[0;32m 689\u001b[0m \"frontend does not support stdin.\") \n\u001b[0;32m 690\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mStdinNotImplementedError\u001b[0m: raw_input was called, but this frontend does not support stdin." ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Boolean type" ] }, { "cell_type": "code", "collapsed": false, "input": [ "var_1 = True\n", "var_2 = False\n", "type( var_1 )\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 2, "text": [ "bool" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "var_1 and var_2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 3, "text": [ "False" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "var_1 or var_2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 4, "text": [ "True" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "not var_1 or var_2\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 5, "text": [ "False" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "More on lists [here](http://docs.python.org/tutorial/datastructures.html#more-on-lists)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a_string = \"1 2 3\" # This is just a string\n", "type ( a_string ) # Test that it is indeed a list" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 14, "text": [ "str" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "type ( a_string.split() ) # Split a string to create a list of elements..." ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 15, "text": [ "list" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "A Python list is an **ordered arrangement** of elements. Note that the elements can have different types. \n", "\n", "Lists are defined either using the `list` keyword or using `[]`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_list = [ 1, 2, \"three\", \"four\", \"5\" ] # A list. In this case, an mix of ints and strs" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "type ( my_list ) # Check the type" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 17, "text": [ "list" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Python, lists **start at 0**, and we refer to the elements using `[i]`, where `i` is the element number" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print my_list [0] # Print the first element\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "print my_list [3] # The fourth element" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "four\n" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "print my_list [ 0:2] # Select from first to third element (non-inclusive)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2]\n" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "print my_list [0:4:2]# Select from 0 to 4, skipping every other element" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 'three']\n" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "print my_list [ -1 ] # -1 is the last element" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "print my_list [ :-3] # Select from element 0 (ommitted) to third from the end" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2]\n" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "print my_list [-3:] # Select from 3rd element from the end to end (ommitted)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['three', 'four', '5']\n" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "help(list) # or see [this](http://docs.python.org/tutorial/datastructures.html#more-on-lists)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Help on class list in module __builtin__:\n", "\n", "class list(object)\n", " | list() -> new empty list\n", " | list(iterable) -> new list initialized from iterable's items\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", " | __delitem__(...)\n", " | x.__delitem__(y) <==> del x[y]\n", " | \n", " | __delslice__(...)\n", " | x.__delslice__(i, j) <==> del x[i:j]\n", " | \n", " | Use of negative indices is not supported.\n", " | \n", " | __eq__(...)\n", " | x.__eq__(y) <==> x==y\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", " | __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", " | __iadd__(...)\n", " | x.__iadd__(y) <==> x+=y\n", " | \n", " | __imul__(...)\n", " | x.__imul__(y) <==> x*=y\n", " | \n", " | __init__(...)\n", " | x.__init__(...) initializes x; see help(type(x)) for signature\n", " | \n", " | __iter__(...)\n", " | x.__iter__() <==> iter(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*n\n", " | \n", " | __ne__(...)\n", " | x.__ne__(y) <==> x!=y\n", " | \n", " | __repr__(...)\n", " | x.__repr__() <==> repr(x)\n", " | \n", " | __reversed__(...)\n", " | L.__reversed__() -- return a reverse iterator over the list\n", " | \n", " | __rmul__(...)\n", " | x.__rmul__(n) <==> n*x\n", " | \n", " | __setitem__(...)\n", " | x.__setitem__(i, y) <==> x[i]=y\n", " | \n", " | __setslice__(...)\n", " | x.__setslice__(i, j, y) <==> x[i:j]=y\n", " | \n", " | Use of negative indices is not supported.\n", " | \n", " | __sizeof__(...)\n", " | L.__sizeof__() -- size of L in memory, in bytes\n", " | \n", " | append(...)\n", " | L.append(object) -- append object to end\n", " | \n", " | count(...)\n", " | L.count(value) -> integer -- return number of occurrences of value\n", " | \n", " | extend(...)\n", " | L.extend(iterable) -- extend list by appending elements from the iterable\n", " | \n", " | index(...)\n", " | L.index(value, [start, [stop]]) -> integer -- return first index of value.\n", " | Raises ValueError if the value is not present.\n", " | \n", " | insert(...)\n", " | L.insert(index, object) -- insert object before index\n", " | \n", " | pop(...)\n", " | L.pop([index]) -> item -- remove and return item at index (default last).\n", " | Raises IndexError if list is empty or index is out of range.\n", " | \n", " | remove(...)\n", " | L.remove(value) -- remove first occurrence of value.\n", " | Raises ValueError if the value is not present.\n", " | \n", " | reverse(...)\n", " | L.reverse() -- reverse *IN PLACE*\n", " | \n", " | sort(...)\n", " | L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\n", " | cmp(x, y) -> -1, 0, 1\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data and other attributes defined here:\n", " | \n", " | __hash__ = None\n", " | \n", " | __new__ = \n", " | T.__new__(S, ...) -> a new object with type S, a subtype of T\n", "\n" ] } ], "prompt_number": 27 }, { "cell_type": "code", "collapsed": false, "input": [ "my_list = [ \"twinkle\", \"little\" ]\n", "my_list.insert ( 0, my_list[0].capitalize() )\n", "my_list.append ( \"star\" )\n", "print my_list" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['Twinkle', 'twinkle', 'little', 'star']\n" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "my_list = [ \"twinkle\" ] *2 + \"little star\".split()\n", "print my_list\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['twinkle', 'twinkle', 'little', 'star']\n" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "print [ ( s== \"star\" and \"bat\" ) or s for s in my_list]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['twinkle', 'twinkle', 'little', 'bat']\n" ] } ], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ "extra=\" How I wonder what you are\"\n", "my_list.extend ( extra.split() )\n", "print my_list" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['twinkle', 'twinkle', 'little', 'star', 'How', 'I', 'wonder', 'what', 'you', 'are']\n" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [ "my_list.remove ( \"you\")\n", "print my_list" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['twinkle', 'twinkle', 'little', 'star', 'How', 'I', 'wonder', 'what', 'are']\n" ] } ], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "my_list.insert ( -1, \"ur\" )\n", "my_list.pop ( -1 )\n", "print my_list\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['twinkle', 'twinkle', 'little', 'star', 'How', 'I', 'wonder', 'what', 'ur']\n" ] } ], "prompt_number": 37 }, { "cell_type": "code", "collapsed": false, "input": [ "len ( my_list )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 38, "text": [ "9" ] } ], "prompt_number": 38 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Exercise\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use `.join()` to join the elements of `my_list` above into a single string again." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Integers" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"1\" + \"1\"\n", "print 1 + 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "11\n", "2\n" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "i = 1\n", "print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 50 }, { "cell_type": "code", "collapsed": false, "input": [ "i += 1\n", "print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] } ], "prompt_number": 51 }, { "cell_type": "code", "collapsed": false, "input": [ "i = i + 1\n", "print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] } ], "prompt_number": 52 }, { "cell_type": "code", "collapsed": false, "input": [ "i *= 2\n", "print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] } ], "prompt_number": 53 }, { "cell_type": "code", "collapsed": false, "input": [ "i = i*2\n", "print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12\n" ] } ], "prompt_number": 54 }, { "cell_type": "code", "collapsed": false, "input": [ "i = i/2\n", "print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] } ], "prompt_number": 55 }, { "cell_type": "code", "collapsed": false, "input": [ "i = i + 1\n", "print i/2 ###!!!" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] } ], "prompt_number": 56 }, { "cell_type": "code", "collapsed": false, "input": [ "print i/2, i % 2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3 1\n" ] } ], "prompt_number": 58 }, { "cell_type": "code", "collapsed": false, "input": [ "print \"This is an integer: %d\" % ( i )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is an integer: 7\n" ] } ], "prompt_number": 62 }, { "cell_type": "code", "collapsed": false, "input": [ "print \"This is the same integer: %03d\" % ( i )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is the same integer: 007\n" ] } ], "prompt_number": 63 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Floating point numbers (floats)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 3.0/2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1.5\n" ] } ], "prompt_number": 64 }, { "cell_type": "code", "collapsed": false, "input": [ "print \"%21.10f\" % ( 1.0e10 + 1e-6)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10000000000.0000019073\n" ] } ], "prompt_number": 65 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Dictionaries\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Dictionaries** are similar to lists, but in this case, the different elements are **unordered** and are accessed by **keys**, rather than by position." ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_dict = { \"key1\": \"something\", \"key2\": \"other\", \"key3\":150. }\n", "print my_dict" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'key3': 150.0, 'key2': 'other', 'key1': 'something'}\n" ] } ], "prompt_number": 66 }, { "cell_type": "code", "collapsed": false, "input": [ "print my_dict[ \"key1\" ]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "something\n" ] } ], "prompt_number": 67 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The keys can be strings, integers, [tuples](http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences)... We can also nest dictionaries inside dictionaries:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "some_other_dict = { \"older_dict\": my_dict, \"some_key\": 42 }\n", "print some_other_dict" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'older_dict': {'key3': 150.0, 'key2': 'other', 'key1': 'something'}, 'some_key': 42}\n" ] } ], "prompt_number": 68 }, { "cell_type": "code", "collapsed": false, "input": [ "my_dict[ \"key3\" ] = my_dict[ \"key3\" ]*3\n", "print some_other_dict" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'older_dict': {'key3': 450.0, 'key2': 'other', 'key1': 'something'}, 'some_key': 42}\n" ] } ], "prompt_number": 69 }, { "cell_type": "code", "collapsed": false, "input": [ "my_dict[ \"key2\" ] = None" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 70 }, { "cell_type": "code", "collapsed": false, "input": [ "print some_other_dict" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'older_dict': {'key3': 450.0, 'key2': None, 'key1': 'something'}, 'some_key': 42}\n" ] } ], "prompt_number": 72 }, { "cell_type": "code", "collapsed": false, "input": [ "modis_bands = ['630-690','780-900','450-520','530-610','1230-1250','1550-1750','2090-2350']\n", "band_numbers = range ( 1, len( modis_bands ) + 1 )\n", "modis = dict ( zip ( band_numbers, modis_bands ) )\n", "for i, m in modis.iteritems(): # Also itervalues, iterkeys, etc...\n", " print i, m" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 630-690\n", "2 780-900\n", "3 450-520\n", "4 530-610\n", "5 1230-1250\n", "6 1550-1750\n", "7 2090-2350\n" ] } ], "prompt_number": 77 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Simple file I/O" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "File access requires to \n", "\n", "1. Opening a file (either as a file to read to, write to, or append to).\n", "2. Looping over the lines, reading or writing lines.\n", "3. Closing the file.\n", "\n", "Let's start defining some data..." ] }, { "cell_type": "code", "collapsed": false, "input": [ "the_data = [[1.,2.,3.,4.,5.],[1.,4.,9.,16.,25.]]\n", "print the_data\n", "print len ( the_data )\n", "print len ( the_data[0] ), len ( the_data[1] )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[1.0, 2.0, 3.0, 4.0, 5.0], [1.0, 4.0, 9.0, 16.0, 25.0]]\n", "2\n", "5 5\n" ] } ], "prompt_number": 79 }, { "cell_type": "code", "collapsed": false, "input": [ "output_file = open ( \"temporary_file.txt\", 'w' )\n", "for i in range ( len( the_data[0] ) ):\n", " output_file.write ( \"%f %f\\n\" % ( the_data[0][i], the_data[1][i] ) )\n", "output_file.close()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 81 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can now use `less` (or `more`) to view the contents of the file `temporary_file.txt`. It should contain the following:\n", "\n", " 1.000000 1.000000\n", " 2.000000 4.000000\n", " 3.000000 9.000000\n", " 4.000000 16.000000\n", " 5.000000 25.000000\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "input_file = open ( \"temporary_file.txt\", 'r' )\n", "input_data = input_file.readlines()\n", "print input_data" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['1.000000 1.000000\\n', '2.000000 4.000000\\n', '3.000000 9.000000\\n', '4.000000 16.000000\\n', '5.000000 25.000000\\n']\n" ] } ], "prompt_number": 82 }, { "cell_type": "code", "collapsed": false, "input": [ "data = []\n", "values = []\n", "for i in xrange ( len ( input_data ) ):\n", " the_line = input_data[i].strip().split()\n", " data.append ( float ( the_line[0] ) )\n", " values.append ( float ( the_line[1] ) )\n", "new_data = [ data, values ]\n", "print \"new_data: \", new_data\n", "print \"the_data: \", the_data\n", "print \"Are they equal?\", new_data == the_data" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "new_data: [[1.0, 2.0, 3.0, 4.0, 5.0], [1.0, 4.0, 9.0, 16.0, 25.0]]\n", "the_data: [[1.0, 2.0, 3.0, 4.0, 5.0], [1.0, 4.0, 9.0, 16.0, 25.0]]\n", "Are they equal? True\n" ] } ], "prompt_number": 84 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }