In class-examples, January 27, 2020

In [2]:
inner=["cat", "dog", "bear"]
In [3]:
otherinner = [1, 2, 3]
In [4]:
mylist = [inner,otherinner]
In [5]:
mylist
Out[5]:
[['cat', 'dog', 'bear'], [1, 2, 3]]
In [6]:
nested = [["a", 1, "b"], ["dog"]]
In [7]:
print(nested)
[['a', 1, 'b'], ['dog']]
In [9]:
print(list(range(5)))
[0, 1, 2, 3, 4]
In [12]:
dummy_input = """5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39"""
In [33]:
numbers = []
names = []
for x in dummy_input.split("\n")[1:]:
    try:
        fl = float(x)
        numbers.append(fl)
    except:
        names.append(x)
print(numbers)
print(names)
[37.21, 37.21, 37.2, 41.0, 39.0]
['Harry', 'Berry', 'Tina', 'Akriti', 'Harsh']
In [34]:
print(list(zip(names, numbers)))
[('Harry', 37.21), ('Berry', 37.21), ('Tina', 37.2), ('Akriti', 41.0), ('Harsh', 39.0)]
In [35]:
def get_students(instring):
    listofstrings = instring.split("\n")[1:]
    names = []
    numbers = []
    for x in listofstrings:
        try:
            fl = float(x)
            numbers.append(fl)
        except:
            names.append(x)
    index_cards = list(zip(names, numbers))
    grades = []
    for x in index_cards:
        grades.append(x[1])
    grades = list(set(grades))
    grades = sorted(grades)
    secondlowest = grades[1]
    almost_flunking = []
    for student in index_cards:
        if student[1] == secondlowest:
            almost_flunking.append(student[0])
    for x in sorted(almost_flunking):
        print(x)
In [36]:
get_students(dummy_input)
Berry
Harry
In [40]:
"angrycat"[5:]
Out[40]:
'cat'
In [ ]:
 

links