diff --git a/Students/Tyler G/session4/dictionaries_sets_lab.py b/Students/Tyler G/session4/dictionaries_sets_lab.py new file mode 100644 index 00000000..14321081 --- /dev/null +++ b/Students/Tyler G/session4/dictionaries_sets_lab.py @@ -0,0 +1,38 @@ +sample_dict = {"name": "Chris", "city": "Seattle", "cake": "Chocolate"} + +del sample_dict['cake'] + +print sample_dict + +sample_dict['fruit'] = "Mango" + +print sample_dict + +print sample_dict.keys() +print sample_dict.values() + +print 'cake' in sample_dict.keys() +print 'Mango' in sample_dict.values() + +nums = list(range(0,15)) +hexidecimal = [0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F"] + +print dict(zip(nums, hexidecimal)) + +for key in sample_dict.keys(): + value = sample_dict[key].count('t') + sample_dict[key] = value + +s2 = set([i for i in range(1,21) if i % 2 == 0]) +s3 = set([i for i in range(1,21) if i % 3 == 0]) +s4 = set([i for i in range(1,21) if i % 4 == 0]) + +print s3.issubset(s2) +print s4.issubset(s2) + +python_set = set("Python") +python_set.add("i") + +marathon_set = frozenset("marathon") +python_set.union(marathon_set) +python_set.intersection(marathon_set) diff --git a/Students/Tyler G/session4/exceptions_lab.py b/Students/Tyler G/session4/exceptions_lab.py new file mode 100644 index 00000000..ef9f9cff --- /dev/null +++ b/Students/Tyler G/session4/exceptions_lab.py @@ -0,0 +1,18 @@ +def safe_input(): + try: + input = raw_input("Try to escape this: ") + except EOFError: + print "That won't work" + return None + except KeyboardInterrupt: + print "You used Ctrl-C or Ctrl-D." + return None + else: + print "Okay that works" + return input + + + + +if __name__ == "__main__": + safe_input() \ No newline at end of file diff --git a/Students/Tyler G/session4/files_lab.py b/Students/Tyler G/session4/files_lab.py new file mode 100644 index 00000000..b5e9a38d --- /dev/null +++ b/Students/Tyler G/session4/files_lab.py @@ -0,0 +1,36 @@ +import os +from collections import Counter + +print "The current working directory is: ", os.getcwd() +print "\n" + +path = 'C:\mystuff\pythonclass\introtopython\examples\session01' +os.chdir(path) + +print "Now the current working directory is: ", os.getcwd() +print "\n" +print "The files in this directory are: ", os.listdir(path) +print "\n" + +file = open("students.txt", "r") +lines = file.readlines() + +languages = [] + +for line in lines[1:]: + for item in line[line.index(':') + 1:].split(', '): + languages.append(item.strip()) + +for index, value in enumerate(languages): + if value == '': + languages.pop(index) + +hash_languages = Counter(languages) + +for key, value in hash_languages.iteritems(): + if key == '': + pass + else: + print "%d student(s) know the '%s' language" % (value, key) + +file.close() \ No newline at end of file diff --git a/Students/Tyler G/session4/sieve of eratosthenes.py b/Students/Tyler G/session4/sieve of eratosthenes.py new file mode 100644 index 00000000..eb677ba8 --- /dev/null +++ b/Students/Tyler G/session4/sieve of eratosthenes.py @@ -0,0 +1,18 @@ +def sieve_eratosthenes(n): + """ + Generate a list of odd integers from 3 to "n". + Use the first element in the list to iterate over the + remaining elements to remove it's multiples. Rinse and repeat. + """ + numbers = range(3, n + 1, 2) + for index, value in enumerate(numbers): + for num in numbers[index + 2:]: + if num % value == 0: + numbers.remove(num) + numbers.append(2) + return sorted(numbers) + +if __name__ == "__main__": + print sieve_eratosthenes(50) + +