Review of fundamentals B

In this notebook, you will review the additional python syntax we've covered since the first review of fundamentals. You should be able to correctly predict what happens in all of these cases. If you get something wrong, figure out why!

Lists, randomization, and list comprehension

my_list = ['spam']
print len(my_list), len(my_list[0])
1 4
print range(10)[-5:]
[5, 6, 7, 8, 9]
import random
my_list = ['a','b','c']*3 + ['c','d','e']*3
random.shuffle(my_list)
my_list[0:5]
Out[3]:
['a', 'c', 'd', 'c', 'd']
import random
my_list = ['a','b','c'] + ['c','d','e']
random.sample(my_list,7)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-131e8d2f92c6> in <module>()
      1 import random
      2 my_list = ['a','b','c'] + ['c','d','e']
----> 3 random.sample(my_list,7)

/Users/glupyan/anaconda2/lib/python2.7/random.pyc in sample(self, population, k)
    321         n = len(population)
    322         if not 0 <= k <= n:
--> 323             raise ValueError("sample larger than population")
    324         random = self.random
    325         _int = int

ValueError: sample larger than population
import random
[round(random.random(),1) for i in range(15)]
Out[12]:
[0.3, 0.0, 0.4, 0.1, 0.1, 0.0, 0.5, 0.2, 0.5, 0.1, 0.0, 0.3, 0.9, 0.9, 0.3]
vowels = ['a', 'e', 'i','o','u']
print [element*i for i,element in enumerate(vowels)]
['', 'e', 'ii', 'ooo', 'uuuu']
print ' '.join([' '.join((i, 'and a')) for i in ('one', 'two', 'three', 'four')])
one and a two and a three and a four and a
responses = 'ccctcttcttttcccttctcccctctctccc'
[i for i,curResp in enumerate(responses) if curResp=='c']
Out[9]:
[0, 1, 2, 4, 7, 12, 13, 14, 17, 19, 20, 21, 22, 24, 26, 28, 29, 30]
import random

positions = ['left', 'middle', 'right']
positions2 = positions

positions.append('bottom')
random.shuffle(positions2)

print positions, positions2
print len(positions), len(positions2)
print positions==positions2
['middle', 'bottom', 'right', 'left'] ['middle', 'bottom', 'right', 'left']
4 4
True

Dictionaries

positions = {'left':(-100,0), 'middle':(0,0), 'right':(100,0)}
[random.choice(positions.keys()) for i in range(10)]
Out[5]:
['left',
 'right',
 'left',
 'middle',
 'left',
 'middle',
 'left',
 'left',
 'left',
 'right']
positions = {'left':(-100,0), 'middle':(0,0), 'right':(100,0)}
[positions[random.choice(positions.keys())] for i in range(10)]
Out[6]:
[(0, 0),
 (0, 0),
 (100, 0),
 (0, 0),
 (-100, 0),
 (100, 0),
 (0, 0),
 (-100, 0),
 (-100, 0),
 (-100, 0)]
responses = 'ccctcttcttttcccttctcccctctctccc'
responsesMapping = {'c':'chair','t':'table'}
[responsesMapping[curResp] for curResp in responses]
Out[7]:
['chair',
 'chair',
 'chair',
 'table',
 'chair',
 'table',
 'table',
 'chair',
 'table',
 'table',
 'table',
 'table',
 'chair',
 'chair',
 'chair',
 'table',
 'table',
 'chair',
 'table',
 'chair',
 'chair',
 'chair',
 'chair',
 'table',
 'chair',
 'table',
 'chair',
 'table',
 'chair',
 'chair',
 'chair']