One Liners

Comprehensions

          
>>> stats = [('foo.com', 'firefox', 23), ('bar.com', 'chrome', 47), ('foo.com', 'chrome', 3), ('bar.com', 'firefox',
16)]
>>>
>>> [tuple[2] for tuple in stats if tuple[0] == 'foo.com']
[23, 3]
>>> sum([tuple[2] for tuple in stats if tuple[0] == 'foo.com'])
26
>>> sum([tuple[2] for tuple in stats if tuple[1] == 'firefox'])
39
          
        

More List Comprehensions

          
>>> nums = range(20)
>>> [abs(num - 10) for num in nums]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [abs(num - 10) for num in nums if num >= 10 and num <= 15]
[0, 1, 2, 3, 4, 5]
>>> pairs = [('zzz', 3), ('bbb', 10), ('ccc', 4), ('aaa', 6)]
>>> [pair[1] for pair in pairs]
[3, 10, 4, 6]
>>> [pair[1] for pair in pairs if not pair[0].startswith('c')]
[3, 10, 6]
>>> [pair[0][0].upper() + pair[0][1:] for pair in pairs]
['Zzz', 'Bbb', 'Ccc', 'Aaa']
          
        

Data Analysis Using Jupyter Notebooks

Download the solution Jupyter notebook (which you can run the same way as the starter code) herefrom the online version of this handout, or view a pdf of said notebook here.


Experimenting with the Debugger

There's no correct answer to this one! When it comes to using the debugger, the journey is the destination.