[10/05/2011] Quiz #1 and Quiz #2 -- pick up at front of room (also blank copies) Quiz #3 today! Thiru's office hours this week ONLY: Fri 10am-12pm (AE 217) (nothing on Thursday) ====================================================================== Python 3.2 (r32:88445, Mar 25 2011, 19:28:28) [GCC 4.5.2] on linux2 Type "copyright", "credits" or "license()" for more information. ==== No Subprocess ==== >>> >>> # sets are collections of 0 or more items with >>> # no duplicates (essentially keys in a dictionary) >>> >>> L = [ 1, 2, 3, 3, 2, 3, 2, 1, 2, 3, 3, 2, 4 ] >>> L [1, 2, 3, 3, 2, 3, 2, 1, 2, 3, 3, 2, 4] >>> set( L ) {1, 2, 3, 4} >>> S = set( L ) >>> S {1, 2, 3, 4} >>> L = [ 5, 6, 4, 3, 3, 2, 3, 3, 4, 5, 4 ] >>> S = set( L ) >>> S {2, 3, 4, 5, 6} >>> S = { 4, 5, 6, 5 } >>> S {4, 5, 6} >>> S = { 5, 5, 6, 2 } >>> S {2, 5, 6} >>> >>> >>> dir(set) ['__and__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update'] >>> S1 = { 1, 3, 5, 7 } >>> S2 = { 2, 4, 6, 8 } >>> S1 {1, 3, 5, 7} >>> S2 {8, 2, 4, 6} >>> S1.add( 9 ) >>> S1 {1, 3, 9, 5, 7} >>> S1.add( 5 ) >>> S1 {1, 3, 9, 5, 7} >>> # S1.clear() >>> S1.difference( S2 ) {7, 1, 3, 5, 9} >>> S1.add( 8 ) >>> S1 {1, 3, 5, 7, 8, 9} >>> S2 {8, 2, 4, 6} >>> S1.difference( S2 ) {1, 3, 9, 5, 7} >>> S1.union( S2 ) {1, 2, 3, 4, 5, 6, 7, 8, 9} >>> S1 {1, 3, 5, 7, 8, 9} >>> S2 {8, 2, 4, 6} >>> S1.difference_update( S2 ) >>> S1 {1, 3, 5, 7, 9} >>> S2 {8, 2, 4, 6} >>> dir(set) ['__and__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update'] >>> >>> >>> S1 {1, 3, 5, 7, 9} >>> S2 {8, 2, 4, 6} >>> >>> >>> S1 = { 1, 2, 3, 4 } >>> S2 {8, 2, 4, 6} >>> S1.difference( S2 ) {1, 3} >>> S1.difference_update( S2 ) >>> S1 {1, 3} >>> >>> >>> S1 = { 1, 3, 4, 5, 6 } >>> S2 {8, 2, 4, 6} >>> S1 & S2 {4, 6} >>> S1 | S2 {1, 2, 3, 4, 5, 6, 8} >>> >>> S1 {1, 3, 4, 5, 6} >>> S2 {8, 2, 4, 6} >>> S1 > S2 False >>> S1 < S2 False >>> >>> >>> S1 = { 1, 2, 3, 4 } >>> S2 = { 2, 3 } >>> S1 {1, 2, 3, 4} >>> S2 {2, 3} >>> S1 > S2 True >>> S1 < S2 False >>> >>> S2 < S1 True >>> # superset and subset operators (above) >>> >>> >>> S1 {1, 2, 3, 4} >>> S2 {2, 3} >>> S1.isdisjoint( S2 ) False >>> S1.isdisjoint( set() ) True >>> S1.isdisjoint( { 5, 6 } ) True >>> >>> L = [ 1, 2, 3, 4, 4, 3, 3, 2, 1, 2, 3, 5 ] >>> L = list( set( L ) ) # remove dups >>> L [1, 2, 3, 4, 5] >>> L = [ 2, 4, 6, 8 ] >>> L = list( set( L ) ) >>> L [8, 2, 4, 6] >>> >>> >>> >>> >>> >>> >>> >>> # an object is "iterable" if it is either stored in a >>> # sequence or it's an object that produces one result >>> # at a time (for loop, etc.) >>> for x in [ 1, 2, 3, 4 ]: print( x ) 1 2 3 4 >>> for x in ( 1, 2, 3, 4 ): print( x, end = '' ) 1234 >>> for x in '1234thestring': print( x, end = '' ) 1234thestring >>> for x in '1234thestring': print( x * 2, end = '' ) 11223344tthheessttrriinngg >>> >>> >>> import SyntaxError: invalid syntax >>> import os >>> os.getcwd() '/home/goldschd' >>> os.chdir( 'CSCI-2961' ) >>> >>> for line in open( 'input.txt' ): print( line.upper(), end = '' ) ABCD EFGH IJKL MNOP >>> L = [ line.upper() for line in open( 'input.txt' ) ] >>> L ['ABCD\n', 'EFGH\n', 'IJKL\n', 'MNOP\n', '\n'] >>> >>> L = [ line.upper().rstrip() for line in open( 'input.txt' ) ] >>> L ['ABCD', 'EFGH', 'IJKL', 'MNOP', ''] >>> >>> # list comprehensions apply expression to all elements >>> # produced by the for loop >>> >>> L ['ABCD', 'EFGH', 'IJKL', 'MNOP', ''] >>> map( str.upper, open( 'input.txt' ) ) >>> list( map( str.upper, open( 'input.txt' ) ) ) ['ABCD\n', 'EFGH\n', 'IJKL\n', 'MNOP\n', '\n'] >>> list( map( str.rstrip, map( str.upper, open( 'input.txt' ) ) ) ) ['ABCD', 'EFGH', 'IJKL', 'MNOP', ''] >>> >>> >>> sorted( open( 'input.txt' ), reverse = True ) ['mnop\n', 'ijkl\n', 'efgh\n', 'abcd\n', '\n'] >>> >>> >>> L ['ABCD', 'EFGH', 'IJKL', 'MNOP', ''] >>> 'ABCD' in L True >>> 'abcd\n' in open( 'input.txt' ) True >>> 'xyz\n' in open( 'input.txt' ) False >>> >>> >>> sum( [ 1, 2, 3, 4 ] ) 10 >>> L ['ABCD', 'EFGH', 'IJKL', 'MNOP', ''] >>> sum( L ) Traceback (most recent call last): File "", line 1, in sum( L ) TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> >>> >>> sum( open( 'numbers.txt' ) ) Traceback (most recent call last): File "", line 1, in sum( open( 'numbers.txt' ) ) TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> sum( [ int( line ) for line in open( 'numbers.txt' ) ] ) 279 >>> >>> numbers = [] >>> for line in open( 'numbers.txt' ): numbers += [ int( line ) ] >>> numbers [45, 67, 78, 89] >>> sum( numbers ) 279 >>> >>> >>> >>> list( open( 'input.txt' ) ) ['abcd\n', 'efgh\n', 'ijkl\n', 'mnop\n', '\n'] >>> tuple( open( 'input.txt' ) ) ('abcd\n', 'efgh\n', 'ijkl\n', 'mnop\n', '\n') >>> >>> '
'.join( open( 'input.txt' ) ) 'abcd\n
efgh\n
ijkl\n
mnop\n
\n' >>> >>> >>> a, b, c, d = open( 'input.txt' ) Traceback (most recent call last): File "", line 1, in a, b, c, d = open( 'input.txt' ) ValueError: too many values to unpack (expected 4) >>> a, b, c, d, e = open( 'input.txt' ) >>> a 'abcd\n' >>> b 'efgh\n' >>> c 'ijkl\n' >>> d 'mnop\n' >>> e '\n' >>> >>> >>> >>> L = [ 1, 2, 3, 4, 5 ] >>> for x in range( len( L ) ): L[x] += 100 >>> L [101, 102, 103, 104, 105] >>> >>> for x in L: x += 100 print( x, end = ' ' ) 201 202 203 204 205 >>> L [101, 102, 103, 104, 105] >>> >>> >>> M = [] >>> for x in L: M.append( x + 100 ) >>> L [101, 102, 103, 104, 105] >>> M [201, 202, 203, 204, 205] >>> >>> >>> L [101, 102, 103, 104, 105] >>> [ x + 10 for x in L ] [111, 112, 113, 114, 115] >>> L [101, 102, 103, 104, 105] >>> # list comprehension: [ for x in L ] >>> >>> L = [ x + 10 for x in L ] >>> L [111, 112, 113, 114, 115] >>> >>> >>> >>> >>> f = open( 'input.txt' ) >>> lines = f.readlines() >>> lines ['abcd\n', 'efgh\n', 'ijkl\n', 'mnop\n', '\n'] >>> >>> lines = [ line.rstrip() for line in lines ] >>> lines ['abcd', 'efgh', 'ijkl', 'mnop', ''] >>> >>> lines = [ line.rstrip() for line in open( 'input.txt' ) ] >>> lines ['abcd', 'efgh', 'ijkl', 'mnop', ''] >>> >>> >>> >>> >>> >>> >>> lines = [ line.rstrip() for line in open( 'input.txt' ) if line[0] == 'e' or line[0] == 'k' ] >>> lines ['efgh'] >>> >>> # list comprehension: >>> # [ for if ] >>> # ^^^^^^^^^ >>> # optional and gives us >>> # a means to filter elements >>> >>> >>> [ x + y for x in 'abc' for y in 'efg' ] ['ae', 'af', 'ag', 'be', 'bf', 'bg', 'ce', 'cf', 'cg'] >>> [ x + y + z for x in 'abc' for y in 'efg' for z in 'hij' ] ['aeh', 'aei', 'aej', 'afh', 'afi', 'afj', 'agh', 'agi', 'agj', 'beh', 'bei', 'bej', 'bfh', 'bfi', 'bfj', 'bgh', 'bgi', 'bgj', 'ceh', 'cei', 'cej', 'cfh', 'cfi', 'cfj', 'cgh', 'cgi', 'cgj'] >>> ['aeh', 'aei', 'aej', 'afh', 'afi', 'afj', 'agh', 'agi', 'agj', 'beh', 'bei', 'bej', 'bfh', 'bfi', 'bfj', 'bgh', 'bgi', 'bgj', 'ceh', 'cei', 'cej', 'cfh', 'cfi', 'cfj', 'cgh', 'cgi', 'cgj'] ['aeh', 'aei', 'aej', 'afh', 'afi', 'afj', 'agh', 'agi', 'agj', 'beh', 'bei', 'bej', 'bfh', 'bfi', 'bfj', 'bgh', 'bgi', 'bgj', 'ceh', 'cei', 'cej', 'cfh', 'cfi', 'cfj', 'cgh', 'cgi', 'cgj'] >>> >>> >>> >>> >>> >>> [ x + y for x in 'david' for y in 'deanna' if x == y ] ['dd', 'aa', 'aa', 'dd'] >>> [ x for x in 'david' for y in 'deanna' if x == y ] ['d', 'a', 'a', 'd'] >>> list( set( [ x for x in 'david' for y in 'deanna' if x == y ] ) ) ['a', 'd'] >>> >>> >>> >>> list( set( [ x for x in 'david' for y in 'deanna' if x != y ] ) ) ['a', 'i', 'd', 'v'] >>> >>> >>> >>> [ x + y for x in 'david' for y in 'deanna' if x != y ] ['de', 'da', 'dn', 'dn', 'da', 'ad', 'ae', 'an', 'an', 'vd', 've', 'va', 'vn', 'vn', 'va', 'id', 'ie', 'ia', 'in', 'in', 'ia', 'de', 'da', 'dn', 'dn', 'da'] >>> >>> >>> >>> def add_bonus( x ): return x + 5 >>> add_bonus( 10 ) 15 >>> L = [ 4, 5, 6, 7 ] >>> list( map( add_bonus, L ) ) [9, 10, 11, 12] >>> L [4, 5, 6, 7] >>> >>> 3 ** 4 81 >>> pow( 3, 4 ) 81 >>> >>> list( map( pow, [ 2, 3, 4 ], [ 5, 6, 7 ] ) ) [32, 729, 16384] >>> >>> >>> >>> def within_range( q ): return ( q > 2 and q < 6 ) >>> within_range( 1 ) False >>> within_range( 5 ) True >>> list( range( -8, 8 ) ) [-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7] >>> list( filter( within_range, range( -8, 8 ) ) ) [3, 4, 5] >>> >>> >>> data = [ -18, 14, 18, 19, 17, 14, -12, 108, 21 ] >>> def valid_data( q ): return ( q > 10 and q < 25 ) >>> list( filter( valid_data, data ) ) [14, 18, 19, 17, 14, 21] >>> >>> data [-18, 14, 18, 19, 17, 14, -12, 108, 21] >>> vdata = list( filter( valid_data, data ) ) >>> vdata [14, 18, 19, 17, 14, 21] >>> set( data ) - set( vdata ) {-12, 108, -18} >>> outliers = list( set( data ) - set( vdata ) ) >>> data [-18, 14, 18, 19, 17, 14, -12, 108, 21] >>> vdata [14, 18, 19, 17, 14, 21] >>> outliers [-12, 108, -18] >>> >>> >>>