{ "metadata": { "name": "", "signature": "sha256:f77ad597c726e07fc2b8d0a32913f4621baadfe119bcb56b1118e292762e8f71" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "# Some command line function" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "pwd" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "u'/Users/matt/Dropbox/Software_Carpentry/Yale_Oct_2014/SWC_Yale_Oct_2014'" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "# Math/arithmetic function\n", "1+1\n", "2**4" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "16" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "print 'This is a string in python'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is a string in python\n" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "# Variable assignment\n", "x=12" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "type(x)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "int" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "id(x)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "4298164432" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "dir(x)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "['__abs__',\n", " '__add__',\n", " '__and__',\n", " '__class__',\n", " '__cmp__',\n", " '__coerce__',\n", " '__delattr__',\n", " '__div__',\n", " '__divmod__',\n", " '__doc__',\n", " '__float__',\n", " '__floordiv__',\n", " '__format__',\n", " '__getattribute__',\n", " '__getnewargs__',\n", " '__hash__',\n", " '__hex__',\n", " '__index__',\n", " '__init__',\n", " '__int__',\n", " '__invert__',\n", " '__long__',\n", " '__lshift__',\n", " '__mod__',\n", " '__mul__',\n", " '__neg__',\n", " '__new__',\n", " '__nonzero__',\n", " '__oct__',\n", " '__or__',\n", " '__pos__',\n", " '__pow__',\n", " '__radd__',\n", " '__rand__',\n", " '__rdiv__',\n", " '__rdivmod__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__rfloordiv__',\n", " '__rlshift__',\n", " '__rmod__',\n", " '__rmul__',\n", " '__ror__',\n", " '__rpow__',\n", " '__rrshift__',\n", " '__rshift__',\n", " '__rsub__',\n", " '__rtruediv__',\n", " '__rxor__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__sub__',\n", " '__subclasshook__',\n", " '__truediv__',\n", " '__trunc__',\n", " '__xor__',\n", " 'bit_length',\n", " 'conjugate',\n", " 'denominator',\n", " 'imag',\n", " 'numerator',\n", " 'real']" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "x.numerator" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "12" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "x.denominator" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "1" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "# Can use help(x) to find out more about how the object \"x\" works" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "y = float(x)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "print y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12.0\n" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "# Can explicitly assign a variable to point to a float object with the\n", "# decimal point.\n", "z = 12." ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "print type(x)\n", "print type(y)\n", "print type(z)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "\n" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "print x/5" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] } ], "prompt_number": 27 }, { "cell_type": "code", "collapsed": false, "input": [ "print x/5." ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2.4\n" ] } ], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "y/5" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 29, "text": [ "2.4" ] } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "# Boolean comparisons\n", "x == y" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 31, "text": [ "True" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "# This is an assertion statement: raises an error if the enclosed statement\n", "# is not true. \n", "assert( x/5 == y/5 )" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "AssertionError", "evalue": "", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32massert\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m5\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m5\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m: " ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "# Python lists can be iterated over, and are defined []\n", "mylist = [x, y, z, 'Hi', 'I am', 'Camille']\n", "print mylist" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[12, 12.0, 12.0, 'Hi', 'I am', 'Camille']\n" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "# for loop in python -- Note, python is whitespace VERY sensitive\n", "for ml in mylist : \n", " print ml" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12\n", "12.0\n", "12.0\n", "Hi\n", "I am\n", "Camille\n" ] } ], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist.count(12.0)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 41, "text": [ "3" ] } ], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist.sort()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 42 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 43, "text": [ "[12, 12.0, 12.0, 'Camille', 'Hi', 'I am']" ] } ], "prompt_number": 43 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist.reverse()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 44 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 45, "text": [ "['I am', 'Hi', 'Camille', 12.0, 12.0, 12]" ] } ], "prompt_number": 45 }, { "cell_type": "code", "collapsed": false, "input": [ "# List accessing and slicing, slices m:n is m through and NOT including n\n", "print mylist[0], mylist[3], mylist[3:5], mylist[1:], mylist[:3]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I am 12.0 [12.0, 12.0] ['Hi', 'Camille', 12.0, 12.0, 12] ['I am', 'Hi', 'Camille']\n" ] } ], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "# Functions in python\n", "# This is a function that takes a list, and prints each element\n", "def print_list(samplelist) :\n", " # Sample use of an assertion to help you check for bugs\n", " assert( type(samplelist) == list )\n", " for sl in samplelist :\n", " print sl" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 55 }, { "cell_type": "code", "collapsed": false, "input": [ "# Use the function\n", "print_list(mylist)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I am\n", "Hi\n", "Camille\n", "12.0\n", "12.0\n", "12\n" ] } ], "prompt_number": 56 }, { "cell_type": "code", "collapsed": false, "input": [ "print_list(x)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "AssertionError", "evalue": "", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint_list\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\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;36mprint_list\u001b[0;34m(samplelist)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mprint_list\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msamplelist\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# Sample use of an assertion to help you check for bugs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32massert\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msamplelist\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mlist\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0msl\u001b[0m \u001b[0;32min\u001b[0m \u001b[0msamplelist\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0msl\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAssertionError\u001b[0m: " ] } ], "prompt_number": 57 }, { "cell_type": "code", "collapsed": false, "input": [ "# Functions in python are actually pretty cool and flexible\n", "def funcwargs(*args) : \n", " # args is now treated as a list inside this function\n", " for arg in args : \n", " print arg" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 62 }, { "cell_type": "code", "collapsed": false, "input": [ "funcwargs(1,2,15,mylist,'hello')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "2\n", "15\n", "['I am', 'Hi', 'Camille', 12.0, 12.0, 12]\n", "hello\n" ] } ], "prompt_number": 63 }, { "cell_type": "code", "collapsed": false, "input": [ "# Function with a return variable\n", "def addme(x,y) : \n", " return x+y" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 65 }, { "cell_type": "code", "collapsed": false, "input": [ "addedvalue = addme(1,15)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 68 }, { "cell_type": "code", "collapsed": false, "input": [ "print addedvalue" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "16\n" ] } ], "prompt_number": 69 }, { "cell_type": "code", "collapsed": false, "input": [ "# Assigning a variable to a function with no return value\n", "fw = funcwargs(1,2,3)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "2\n", "3\n" ] } ], "prompt_number": 73 }, { "cell_type": "code", "collapsed": false, "input": [ "type(fw)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 75, "text": [ "NoneType" ] } ], "prompt_number": 75 }, { "cell_type": "code", "collapsed": false, "input": [ "# Quick exercise: Write a function that takes in any arbitrary arguments, \n", "# prints the type of each argument, and if the type is a list, print the \n", "# length of the list" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 76 }, { "cell_type": "code", "collapsed": false, "input": [ "if x==y :\n", " print addme(x,y)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "24.0\n" ] } ], "prompt_number": 77 }, { "cell_type": "code", "collapsed": false, "input": [ "# Note: \"=\" is an assignment, and \"==\" a test (boolean statement)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 78 }, { "cell_type": "code", "collapsed": false, "input": [ "# Exercise solution:\n", "def find_list(*args) : \n", " # args is now a list of everything you fed into this function that was\n", " # comma delimeted i.e. find_list(arg1,arg2,....) --> args = [arg1,arg2...]\n", " for arg in args : \n", " if type(arg) == list :\n", " print len(arg)\n", " " ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 79 }, { "cell_type": "code", "collapsed": false, "input": [ "find_list(1,2,[1,2,3])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] } ], "prompt_number": 80 }, { "cell_type": "code", "collapsed": false, "input": [ "# Dictionaries - YAY! Defined within {}, and these map some key word to a \n", "# value of some other type\n", "mydict = {'xint':x, 'yfloat':y, 'mylist': mylist, 0: 'The 0th key word' }" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 85 }, { "cell_type": "code", "collapsed": false, "input": [ "print mydict['xint'] \n", "print mydict[0]\n", "print mydict" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "12\n", "The 0th key word\n", "{0: 'The 0th key word', 'yfloat': 12.0, 'mylist': ['I am', 'Hi', 'Camille', 12.0, 12.0, 12], 'xint': 12}\n" ] } ], "prompt_number": 87 }, { "cell_type": "code", "collapsed": false, "input": [ "# A difference between dictionaries and lists is that dictionaries do NOT \n", "# preserve order, and lists do.\n", "print mylist" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['I am', 'Hi', 'Camille', 12.0, 12.0, 12]\n" ] } ], "prompt_number": 90 }, { "cell_type": "code", "collapsed": false, "input": [ "# Sample function with key word arguments\n", "def funcwkwargs(float1, float2, shortlist=[1,2], newint=11) :\n", " print len(shortlist)\n", " print newint+1\n", " print float1+float2" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 95 }, { "cell_type": "code", "collapsed": false, "input": [ "funcwkwargs( newint=15,shortlist=[1,2,'a','b'] , float1=12., float2=15.)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4\n", "16\n", "27.0\n" ] } ], "prompt_number": 98 }, { "cell_type": "code", "collapsed": false, "input": [ "# Sample function with arbitrary key word arguments\n", "def arbkwargs(**kwargs) :\n", " # Here, kwargs gets \"unpacked\" as a dictionary\n", " for kw,arg in kwargs.iteritems() : \n", " print kw, arg\n", " print arg" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 107 }, { "cell_type": "code", "collapsed": false, "input": [ "arbkwargs(kwarg1=12, kwarg2='hello')\n", "# This gets unpacked to look like: kwargs = {'kwarg1':12, 'kwarg2': 'hello}" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "kwarg1 12\n", "12\n", "kwarg2 hello\n", "hello\n" ] } ], "prompt_number": 108 }, { "cell_type": "code", "collapsed": false, "input": [ "# Let's take a closer look at the dictionary\n", "mydict.keys()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 112, "text": [ "[0, 'yfloat', 'mylist', 'xint']" ] } ], "prompt_number": 112 }, { "cell_type": "code", "collapsed": false, "input": [ "# Any dictionary object, such as mydict, will have built in \"methods\" \n", "# which are really just functions that are internal to this object. \n", "# Methods have associated () when you call them." ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 115 }, { "cell_type": "code", "collapsed": false, "input": [ "print mydict.keys" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 116 }, { "cell_type": "code", "collapsed": false, "input": [ "# This is an attribute of the object x (which is an int object). Note, \n", "# no parenthesis needed here, because x.numerator simply already points\n", "# to the value 12.\n", "x.numerator" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 118, "text": [ "12" ] } ], "prompt_number": 118 }, { "cell_type": "code", "collapsed": false, "input": [ "# Exercise: Create a function that takes in an arbitrary number of args\n", "# AND kwargs, and print a sorted list of the key words.\n", "# If I use your function \n", "# arbitrary_argkwarg(1, 'hello', Matt='teacher2', Camille='teacher1')\n", "# I should get back \n", "# Camille\n", "# Matt\n", "# Bonus : Print a list of the key words in order of the sorted values." ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 123 }, { "cell_type": "code", "collapsed": false, "input": [ "def arbitrary_argkwarg(*args,**kwargs) :\n", " keys = kwargs.keys()\n", " keys.sort()\n", " print keys" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 152 }, { "cell_type": "raw", "metadata": {}, "source": [] }, { "cell_type": "code", "collapsed": false, "input": [ "arbitrary_argkwarg(1,'hello', Matt='teacher2', Camille='teacher1',Wendell='TA', Dan='TA')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['Camille', 'Dan', 'Matt', 'Wendell']\n" ] } ], "prompt_number": 153 }, { "cell_type": "code", "collapsed": false, "input": [ "#mydict.keys()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 142 }, { "cell_type": "code", "collapsed": false, "input": [ "help(list.sort)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Help on method_descriptor:\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" ] } ], "prompt_number": 149 }, { "cell_type": "code", "collapsed": false, "input": [ "cat data1.txt" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Matthew 34234 934\r\n", "Camille 495 42390\r\n", "Matthew 3294 490\r\n", "Wendell 9403 3049\r\n", "Jieming 39240 30249\r\n" ] } ], "prompt_number": 160 }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a file handle\n", "f = open('data1.txt','r') \n", "# 'r' stands for read, we can also 'a' append, and\n", "# 'w' write" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 178 }, { "cell_type": "code", "collapsed": false, "input": [ "# Iterate over a file\n", "for line in f : \n", " print line\n", " print type(line)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Matthew 34234 934\n", "\n", "\n", "Camille 495 42390\n", "\n", "\n", "Matthew 3294 490\n", "\n", "\n", "Wendell 9403 3049\n", "\n", "\n", "Jieming 39240 30249\n", "\n", "\n" ] } ], "prompt_number": 179 }, { "cell_type": "code", "collapsed": false, "input": [ "# Note, there is an implied newline character at the end of each line\n", "# in the file that is read in\n", "print 'hi\\n'\n", "print 'camille'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "hi\n", "\n", "camille\n" ] } ], "prompt_number": 186 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring = 'hi, my name is Camille'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 189 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring.capitalize()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 191, "text": [ "'Hi, my name is camille'" ] } ], "prompt_number": 191 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 192, "text": [ "'hi, my name is Camille'" ] } ], "prompt_number": 192 }, { "cell_type": "code", "collapsed": false, "input": [ "type(mystring.split('m'))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 195, "text": [ "list" ] } ], "prompt_number": 195 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 196, "text": [ "'hi, my name is Camille'" ] } ], "prompt_number": 196 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring.split()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 199, "text": [ "['hi,', 'my', 'name', 'is', 'Camille']" ] } ], "prompt_number": 199 }, { "cell_type": "code", "collapsed": false, "input": [ "# Let's make a function to read in a file and do stuff with it\n", "def funcread(fname) : \n", " # Create a file handle so I can read the file\n", " f = open(fname,'r')\n", " for line in f : \n", " # Here's where I do stuff...\n", " manage_line(line)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 207 }, { "cell_type": "code", "collapsed": false, "input": [ "# Let's create a function to do stuff to the line\n", "def manage_line(line) :\n", " # Splitting the line returns a list of strings that have been split\n", " # by some value (default white space), then print if the second value\n", " # is greater than the first\n", " splitline = line.split()\n", " if int(splitline[1]) > int(splitline[2]) :\n", " print line\n", " return line" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 222 }, { "cell_type": "code", "collapsed": false, "input": [ "# Testing out manage_line\n", "def test_manage_line() :\n", " # A good reason for manage_line to have a return value is to make sure\n", " # that the function does what you think it should do when you give it\n", " # a known argument\n", " assert(manage_line('Matthew 34234 934') != NoneType)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 224 }, { "cell_type": "code", "collapsed": false, "input": [ "# Run funcread\n", "funcread('data1.txt')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['Matthew', '34234', '934']\n", "['Camille', '495', '42390']\n", "['Matthew', '3294', '490']\n", "['Wendell', '9403', '3049']\n", "['Jieming', '39240', '30249']\n" ] } ], "prompt_number": 209 }, { "cell_type": "code", "collapsed": false, "input": [ "# Exercise : Read in data1.txt, print if the second column is greater than\n", "# the third column. Hint: You can individually access each item in the\n", "# line using the split method, and you will need to recognize that you now\n", "# have a list of strings once you've split that line. Double hint: Modify\n", "# the manage_line function, and manage_line needs to be (re-)defined \n", "# before the funcread function is (re-)defined" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 214 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 225, "text": [ "['I am', 'Hi', 'Camille', 12.0, 12.0, 12]" ] } ], "prompt_number": 225 }, { "cell_type": "code", "collapsed": false, "input": [ "for x in mydict:\n", " print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "yfloat\n", "mylist\n", "xint\n" ] } ], "prompt_number": 231 }, { "cell_type": "code", "collapsed": false, "input": [ "myset = {2,1,3,3}" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 236 }, { "cell_type": "code", "collapsed": false, "input": [ "myset" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 237, "text": [ "{1, 2, 3}" ] } ], "prompt_number": 237 }, { "cell_type": "code", "collapsed": false, "input": [ "myset.add(5)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 238 }, { "cell_type": "code", "collapsed": false, "input": [ "myset" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 239, "text": [ "{1, 2, 3, 5}" ] } ], "prompt_number": 239 }, { "cell_type": "code", "collapsed": false, "input": [ "myset.add(2)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 240 }, { "cell_type": "code", "collapsed": false, "input": [ "myset" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 241, "text": [ "{1, 2, 3, 5}" ] } ], "prompt_number": 241 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist[4]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 242, "text": [ "12.0" ] } ], "prompt_number": 242 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist[1:4]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 243, "text": [ "['Hi', 'Camille', 12.0]" ] } ], "prompt_number": 243 }, { "cell_type": "code", "collapsed": false, "input": [ "2 in mylist" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 244, "text": [ "False" ] } ], "prompt_number": 244 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 245, "text": [ "['I am', 'Hi', 'Camille', 12.0, 12.0, 12]" ] } ], "prompt_number": 245 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist.sort()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 246 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 247, "text": [ "[12.0, 12.0, 12, 'Camille', 'Hi', 'I am']" ] } ], "prompt_number": 247 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 248, "text": [ "'hi, my name is Camille'" ] } ], "prompt_number": 248 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring[1:7]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 252, "text": [ "'i, my '" ] } ], "prompt_number": 252 }, { "cell_type": "code", "collapsed": false, "input": [ "for c in mystring:\n", " print c" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "h\n", "i\n", ",\n", " \n", "m\n", "y\n", " \n", "n\n", "a\n", "m\n", "e\n", " \n", "i\n", "s\n", " \n", "C\n", "a\n", "m\n", "i\n", "l\n", "l\n", "e\n" ] } ], "prompt_number": 253 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 254, "text": [ "'hi, my name is Camille'" ] } ], "prompt_number": 254 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring.replace('h','H')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 255, "text": [ "'Hi, my name is Camille'" ] } ], "prompt_number": 255 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 256, "text": [ "'hi, my name is Camille'" ] } ], "prompt_number": 256 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 257, "text": [ "[12.0, 12.0, 12, 'Camille', 'Hi', 'I am']" ] } ], "prompt_number": 257 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist[0] = 121.234234" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 258 }, { "cell_type": "code", "collapsed": false, "input": [ "mylist" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 259, "text": [ "[121.234234, 12.0, 12, 'Camille', 'Hi', 'I am']" ] } ], "prompt_number": 259 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 260, "text": [ "'hi, my name is Camille'" ] } ], "prompt_number": 260 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring[0] = 'H'" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "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[0mmystring\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'H'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "prompt_number": 261 }, { "cell_type": "code", "collapsed": false, "input": [ "'H' + mystring[1:]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 262, "text": [ "'Hi, my name is Camille'" ] } ], "prompt_number": 262 }, { "cell_type": "code", "collapsed": false, "input": [ "'H' + 'i, my name is Camille'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 263, "text": [ "'Hi, my name is Camille'" ] } ], "prompt_number": 263 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 264, "text": [ "'hi, my name is Camille'" ] } ], "prompt_number": 264 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring = 'dfkjsadflk'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 265 }, { "cell_type": "code", "collapsed": false, "input": [ "mystring" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 266, "text": [ "'dfkjsadflk'" ] } ], "prompt_number": 266 }, { "cell_type": "code", "collapsed": false, "input": [ "mytuple = (1,2,'hello')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 268 }, { "cell_type": "code", "collapsed": false, "input": [ "mytuple" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 269, "text": [ "(1, 2, 'hello')" ] } ], "prompt_number": 269 }, { "cell_type": "code", "collapsed": false, "input": [ "mytuple[1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 271, "text": [ "2" ] } ], "prompt_number": 271 }, { "cell_type": "code", "collapsed": false, "input": [ "mytuple[1] = 234" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "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[0mmytuple\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m234\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "prompt_number": 272 }, { "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": 275 }, { "cell_type": "code", "collapsed": false, "input": [ "'hello\\n'.replace('\\n','')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 276, "text": [ "'hello'" ] } ], "prompt_number": 276 }, { "cell_type": "code", "collapsed": false, "input": [ "'hello\\n'.rstrip('\\n')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 277, "text": [ "'hello'" ] } ], "prompt_number": 277 }, { "cell_type": "code", "collapsed": false, "input": [ "NoneType" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'NoneType' is not defined", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\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[0mNoneType\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'NoneType' is not defined" ] } ], "prompt_number": 278 }, { "cell_type": "code", "collapsed": false, "input": [ "type(None)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 279, "text": [ "NoneType" ] } ], "prompt_number": 279 }, { "cell_type": "code", "collapsed": false, "input": [ "type(set())" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 282, "text": [ "set" ] } ], "prompt_number": 282 }, { "cell_type": "code", "collapsed": false, "input": [ "type({1,3})" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 283, "text": [ "set" ] } ], "prompt_number": 283 }, { "cell_type": "code", "collapsed": false, "input": [ "ValueError" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 284, "text": [ "ValueError" ] } ], "prompt_number": 284 }, { "cell_type": "code", "collapsed": false, "input": [ "float('dskfjsdlfk')" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "could not convert string to float: dskfjsdlfk", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\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[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'dskfjsdlfk'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: could not convert string to float: dskfjsdlfk" ] } ], "prompt_number": 285 }, { "cell_type": "code", "collapsed": false, "input": [ "help(set)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Help on class set in module __builtin__:\n", "\n", "class set(object)\n", " | set() -> new empty set object\n", " | set(iterable) -> new set object\n", " | \n", " | Build an unordered collection of unique elements.\n", " | \n", " | Methods defined here:\n", " | \n", " | __and__(...)\n", " | x.__and__(y) <==> x&y\n", " | \n", " | __cmp__(...)\n", " | x.__cmp__(y) <==> cmp(x,y)\n", " | \n", " | __contains__(...)\n", " | x.__contains__(y) <==> y in x.\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", " | __gt__(...)\n", " | x.__gt__(y) <==> x>y\n", " | \n", " | __iand__(...)\n", " | x.__iand__(y) <==> x&=y\n", " | \n", " | __init__(...)\n", " | x.__init__(...) initializes x; see help(type(x)) for signature\n", " | \n", " | __ior__(...)\n", " | x.__ior__(y) <==> x|=y\n", " | \n", " | __isub__(...)\n", " | x.__isub__(y) <==> x-=y\n", " | \n", " | __iter__(...)\n", " | x.__iter__() <==> iter(x)\n", " | \n", " | __ixor__(...)\n", " | x.__ixor__(y) <==> x^=y\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", " | __or__(...)\n", " | x.__or__(y) <==> x|y\n", " | \n", " | __rand__(...)\n", " | x.__rand__(y) <==> y&x\n", " | \n", " | __reduce__(...)\n", " | Return state information for pickling.\n", " | \n", " | __repr__(...)\n", " | x.__repr__() <==> repr(x)\n", " | \n", " | __ror__(...)\n", " | x.__ror__(y) <==> y|x\n", " | \n", " | __rsub__(...)\n", " | x.__rsub__(y) <==> y-x\n", " | \n", " | __rxor__(...)\n", " | x.__rxor__(y) <==> y^x\n", " | \n", " | __sizeof__(...)\n", " | S.__sizeof__() -> size of S in memory, in bytes\n", " | \n", " | __sub__(...)\n", " | x.__sub__(y) <==> x-y\n", " | \n", " | __xor__(...)\n", " | x.__xor__(y) <==> x^y\n", " | \n", " | add(...)\n", " | Add an element to a set.\n", " | \n", " | This has no effect if the element is already present.\n", " | \n", " | clear(...)\n", " | Remove all elements from this set.\n", " | \n", " | copy(...)\n", " | Return a shallow copy of a set.\n", " | \n", " | difference(...)\n", " | Return the difference of two or more sets as a new set.\n", " | \n", " | (i.e. all elements that are in this set but not the others.)\n", " | \n", " | difference_update(...)\n", " | Remove all elements of another set from this set.\n", " | \n", " | discard(...)\n", " | Remove an element from a set if it is a member.\n", " | \n", " | If the element is not a member, do nothing.\n", " | \n", " | intersection(...)\n", " | Return the intersection of two or more sets as a new set.\n", " | \n", " | (i.e. elements that are common to all of the sets.)\n", " | \n", " | intersection_update(...)\n", " | Update a set with the intersection of itself and another.\n", " | \n", " | isdisjoint(...)\n", " | Return True if two sets have a null intersection.\n", " | \n", " | issubset(...)\n", " | Report whether another set contains this set.\n", " | \n", " | issuperset(...)\n", " | Report whether this set contains another set.\n", " | \n", " | pop(...)\n", " | Remove and return an arbitrary set element.\n", " | Raises KeyError if the set is empty.\n", " | \n", " | remove(...)\n", " | Remove an element from a set; it must be a member.\n", " | \n", " | If the element is not a member, raise a KeyError.\n", " | \n", " | symmetric_difference(...)\n", " | Return the symmetric difference of two sets as a new set.\n", " | \n", " | (i.e. all elements that are in exactly one of the sets.)\n", " | \n", " | symmetric_difference_update(...)\n", " | Update a set with the symmetric difference of itself and another.\n", " | \n", " | union(...)\n", " | Return the union of sets as a new set.\n", " | \n", " | (i.e. all elements that are in either set.)\n", " | \n", " | update(...)\n", " | Update a set with the union of itself and others.\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": 286 }, { "cell_type": "code", "collapsed": false, "input": [ "'C' in {'A', 'B'}" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 289, "text": [ "False" ] } ], "prompt_number": 289 }, { "cell_type": "code", "collapsed": false, "input": [ "{1,2,45}.difference({2,3,4})" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 292, "text": [ "{1, 45}" ] } ], "prompt_number": 292 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }