In [1]:
xs = [10,7,8,5,2,9]
In [2]:
min(xs)
Out[2]:
2
In [5]:
shift = xs.index(min(xs))
print(shift)
4

Dictionary¶

In [6]:
# dictionary is a set of key-value pairs
# keys are usually strings or numbers
# values can be any type
In [13]:
d = {}  # empty dictionary
with open("grades.txt") as f:
    content = f.read()
data = content.split('\n')
#print(data)
for x in data:
    s = x.split(':')
    #print(s)
    if s[0] not in d:
        d[s[0]] = [(s[1],int(s[2]),s[3])]
    else:
        d[s[0]].append((s[1],int(s[2]),s[3]))
print(d)   
{'Jones': [('CSC 1301', 4, 'A'), ('CSC 1302', 4, 'A'), ('CSC 2720', 3, 'B')], 'Smith': [('CSC 1301', 4, 'B'), ('CSC 1302', 4, 'B'), ('CSC 2720', 3, 'A')], 'Blake': [('CSC 1301', 4, 'B'), ('CSC 1301', 4, 'B'), ('CSC 1301', 4, 'B'), ('CSC 1301', 4, 'B')]}
In [20]:
def num_grade(letter_grade):
    nd = {'A':4, 'B':3, 'C':2, 'D':1, 'F':0}
    return nd[letter_grade]
    
def gpa(d, sname):
    if sname in d:
        courses = d[sname]
        ncredits = 0
        npoints = 0
        for course in courses:
            ncredits = ncredits + course[1]
            npoints = npoints + course[1]*num_grade(course[2])
        return npoints/ncredits
    else:
        return None
In [25]:
print(gpa(d,"Sunderraman"))
None
In [15]:
list(d.keys())
Out[15]:
['Jones', 'Smith', 'Blake']
In [16]:
list(d.values())
Out[16]:
[[('CSC 1301', 4, 'A'), ('CSC 1302', 4, 'A'), ('CSC 2720', 3, 'B')],
 [('CSC 1301', 4, 'B'), ('CSC 1302', 4, 'B'), ('CSC 2720', 3, 'A')],
 [('CSC 1301', 4, 'B'),
  ('CSC 1301', 4, 'B'),
  ('CSC 1301', 4, 'B'),
  ('CSC 1301', 4, 'B')]]
In [19]:
list(d.items())
Out[19]:
[('Jones', [('CSC 1301', 4, 'A'), ('CSC 1302', 4, 'A'), ('CSC 2720', 3, 'B')]),
 ('Smith', [('CSC 1301', 4, 'B'), ('CSC 1302', 4, 'B'), ('CSC 2720', 3, 'A')]),
 ('Blake',
  [('CSC 1301', 4, 'B'),
   ('CSC 1301', 4, 'B'),
   ('CSC 1301', 4, 'B'),
   ('CSC 1301', 4, 'B')])]
In [ ]: