90 likes | 193 Vues
CSCI 1001. overview of. computer science. PYTHON V. ages = [20, 18, 17, 26]. 17 in ages. ages.index(17). sum(ages ). range(0,10,2). ‘ abc ’ in ‘ abcdefg ’. ‘how are you’[4:7]. s = ‘Mr. Spock’. s[0:2] = ‘Dr’ #Won’t work. ‘do re mi fa so’.find(‘mi ’).
E N D
CSCI 1001 overview of computer science PYTHON V
ages = [20, 18, 17, 26] 17 in ages ages.index(17) sum(ages) range(0,10,2)
‘abc’ in ‘abcdefg’ ‘how are you’[4:7] s = ‘Mr. Spock’ s[0:2] = ‘Dr’ #Won’t work ‘do re mi faso’.find(‘mi’) for s in ‘i can hazcheez’: print s.upper(),
d = {} # empty dictionary d = {‘nishi’:4, ‘lnp’:1} d[‘nishi’] d[‘neshek’] # KeyError ‘neshek’ in d d[‘neshek’] = 1 len(d)
d = {‘nishi’:4, ‘lnp’:1} d.keys() d.values() d.items() del d[‘lnp’] # in St. Louis dict.fromkeys([1,2,3],’a’)
def common(alice, bob): cs = [] for a in alice: if a in bob: cs.append(a) return cs def common2(as, bs): cs = [] d = dict.fromkeys(bs,True) for a in as: if a in d: cs.append(a) return cs
fileName = raw_input(“file name: ") f = open(fileName) sum = 0 numLines = 0 for currentLine in f: sum = sum + len(currentLine) numLines = numLines + 1 print "Number of lines: ", numLines if numLines != 0: print "Avg length:", sum/numLines
import urllib url = raw_input(“input the url: ") u = urllib.urlopen(url) sum = 0 numLines = 0 for currentLine in u: sum = sum + len(currentLine) numLines = numLines + 1 print "Number of lines: ", numLines if numLines != 0: print "Average line length: ", sum/numLines
http://cs1001.us/ Please read Sections 3.3 and 3.4 for Wednesday