3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] Python Type "help", "copyright", "credits" or "license" for more information. [evaluate lec14.py] x Traceback (most recent call last): Python Shell, prompt 2, line 1 builtins.NameError: name 'x' is not defined y 5 [evaluate lec14.py] y 5 [evaluate lec14.py] y 5 x 5 [evaluate lec14.py] this is a line of text a second line line 3 [evaluate lec14.py] Traceback (most recent call last): File "/Users/sean/Dropbox/lecture/intro/lec14/lec14.py", line 8, in main() File "/Users/sean/Dropbox/lecture/intro/lec14/lec14.py", line 4, in file = open('myfilee.txt') builtins.FileNotFoundError: [Errno 2] No such file or directory: 'myfilee.txt' [evaluate lec14.py] this is a line of text a second line line 3 file = open('myfile.txt') file.read(3) 'thi' file.read(3) 's i' file.read(5) 's a l' file.read(8) 'ine of t' file.close() file = open('myfile.txt') file.read(8) 'this is ' [evaluate lec14.py] Portland 67 Lewiston 36 Bangor 33 SouthPortland 25 Auburn 23 Biddeford 21 Sanford 21 Brunswick 20 Scarborough 20 Saco 19 file = open('population.txt') # list of all the lines in the file lines = file.readlines() type(lines) lines[0] 'Portland 67\n' lines[1] 'Lewiston 36\n' print('some string') some string print('some string\n') some string [evaluate lec14.py] Portland 67 Lewiston 36 Bangor 33 SouthPortland 25 Auburn 23 Biddeford 21 Sanford 21 Brunswick 20 Scarborough 20 Saco 1 [evaluate lec14.py] Portland 67 Lewiston 36 Bangor 33 SouthPortland 25 Auburn 23 Biddeford 21 Sanford 21 Brunswick 20 Scarborough 20 Saco 1 [evaluate lec14.py] Portland 67 Lewiston 36 Bangor 33 SouthPortland 25 Auburn 23 Biddeford 21 Sanford 21 Brunswick 20 Scarborough 20 Saco 19 [evaluate lec14.py] Portland 67 Lewiston 36 Bangor 33 SouthPortland 25 Auburn 23 Biddeford 21 Sanford 21 Brunswick 20 Scarborough 20 Saco 1 [evaluate lec14.py] Portland 67 Lewiston 36 Bangor 33 SouthPortland 25 Auburn 23 Biddeford 21 Sanford 21 Brunswick 20 Scarborough 20 Saco 19 'some string '.strip() 'some string' ' some string '.strip() 'some string' [evaluate lec14.py] Traceback (most recent call last): File "/Users/sean/Dropbox/lecture/intro/lec14/lec14.py", line 23, in main() File "/Users/sean/Dropbox/lecture/intro/lec14/lec14.py", line 17, in totalpop += citypop builtins.TypeError: unsupported operand type(s) for +=: 'int' and 'str' [evaluate lec14.py] Total population is 285 [evaluate lec14.py] Total population is 285 [evaluate lec14.py] Total population is 136 [evaluate lec14.py] Total population is 184 file = open('population.txt', 'w') file.write('computer') 8 file.close() file = open('population.txt', 'w') file.write('science') 7 file.close() file = open('population.txt', 'a') file.write('more stuff') 10 file.close() file = open('population.txt', 'w') file.write('Town1 123\n') 10 file.write('AnotherTown 80\n') 15 file.write('AnotherTown 30\n') 15 file.close() newfile = open('newfilename.txt Traceback (most recent call last): Python Shell, prompt 69, line 1 Syntax Error: newfile = open('newfilename.txt: , line 1, pos 32 newfile = open('newfilename.txt', 'w') newfile.write('some text here') 14 newfile.close() cs = ['Alice', 'Charlie', 'Denise'] cs ['Alice', 'Charlie', 'Denise'] bio = ['Denise', 'Eva', 'Fred'] chem = ['Bob', 'Fred', 'Alice'] bio ['Denise', 'Eva', 'Fred'] cs + bio ['Alice', 'Charlie', 'Denise', 'Denise', 'Eva', 'Fred'] len(cs + bio) 6 cs = {'Alice', 'Charlie', 'Denise'} bio = {'Denise', 'Eva', 'Fred'} chem = {'Bob', 'Fred', 'Alice'} type(cs) cs {'Alice', 'Charlie', 'Denise'} bio {'Fred', 'Eva', 'Denise'} chem {'Fred', 'Bob', 'Alice'} cs[0] Traceback (most recent call last): Python Shell, prompt 87, line 1 builtins.TypeError: 'set' object does not support indexing bio[2] Traceback (most recent call last): Python Shell, prompt 88, line 1 builtins.TypeError: 'set' object does not support indexing cs = ['Alice', 'Charlie', 'Denise'] cs[0] 'Alice' cs = set(cs) type(cs) cs[0] Traceback (most recent call last): Python Shell, prompt 93, line 1 builtins.TypeError: 'set' object does not support indexing set('Bowdoin') {'n', 'o', 'i', 'w', 'B', 'd'} len(set('Bowdoin')) 6 cs {'Alice', 'Charlie', 'Denise'} len(cs) 3 empty = set() empty set() len(empty) 0 empty_list = [] empty_set = {} [evaluate lec14.py] remove_list_duplicates( ' Traceback (most recent call last): Python Shell, prompt 105, line 3 Syntax Error: ': , line 3, pos 2 remove_list_duplicates(['a', 'b', 'b', 'c', 'd', 'd']) ['c', 'd', 'b', 'a'] cs {'Alice', 'Charlie', 'Denise'} chem {'Fred', 'Bob', 'Alice'} bio {'Fred', 'Eva', 'Denise'} 'Alice' in cs True 'Bob' in cs False cs_list = ['Alice', 'Charlie', 'Denise'] 'Alice' in cs_list True 'Bob' in cs_list False [evaluate settest.py] Running function with list... Located 2000 numbers Function took 0.0366 secs Running function with set... Located 2000 numbers Function took 0.000419 secs [evaluate settest.py] Running function with list... Located 5000 numbers Function took 0.228 secs Running function with set... Located 5000 numbers Function took 0.00141 secs [evaluate settest.py] Running function with list... Located 10000 numbers Function took 0.817 secs Running function with set... Located 10000 numbers Function took 0.00146 secs [evaluate settest.py] Running function with list... Located 20000 numbers Function took 3.15 secs Running function with set... Located 20000 numbers Function took 0.00326 secs [evaluate settest.py] Running function with list... Located 25000 numbers Function took 4.9 secs Running function with set... Located 25000 numbers Function took 0.00329 secs cs {'Alice', 'Charlie', 'Denise'} bio {'Fred', 'Eva', 'Denise'} chem {'Fred', 'Bob', 'Alice'} cs.add('Bob') cs {'Bob', 'Alice', 'Charlie', 'Denise'} [1, 2] + [3] [1, 2, 3] {1, 2} + {3} Traceback (most recent call last): Python Shell, prompt 131, line 1 builtins.TypeError: unsupported operand type(s) for +: 'set' and 'set' cs + bio Traceback (most recent call last): Python Shell, prompt 132, line 1 builtins.TypeError: unsupported operand type(s) for +: 'set' and 'set' cs - bio {'Bob', 'Alice', 'Charlie'} cs {'Bob', 'Alice', 'Charlie', 'Denise'} bio {'Fred', 'Eva', 'Denise'} cs - bio {'Bob', 'Alice', 'Charlie'} cs {'Bob', 'Alice', 'Charlie', 'Denise'} bio {'Fred', 'Eva', 'Denise'} cs.remove('Bob') cs {'Alice', 'Charlie', 'Denise'}