Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Students/Tyler G/session4/dictionaries_sets_lab.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions Students/Tyler G/session4/exceptions_lab.py
Original file line number Diff line number Diff line change
@@ -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()
36 changes: 36 additions & 0 deletions Students/Tyler G/session4/files_lab.py
Original file line number Diff line number Diff line change
@@ -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()
18 changes: 18 additions & 0 deletions Students/Tyler G/session4/sieve of eratosthenes.py
Original file line number Diff line number Diff line change
@@ -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)