90 likes | 199 Views
This overview covers foundational concepts in computer science with a focus on Python programming. Topics include data structures like dictionaries, string manipulation, and file handling. Learn how to process data, find common elements between lists, and navigate through URLs. This course is designed for beginners and covers practical examples to illustrate programming concepts. Additionally, it touches on error handling and average calculations to give students essential skills for future programming endeavors. Perfect for ages 17 and up!
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