In [1]:
def add(x,y):
  return x + y

In [2]:
add(5,6)


Out[2]:
11

In [3]:
l = [1,2,"abc"]

In [4]:
type(l)


Out[4]:
list

In [5]:
l.append('def')

In [6]:
l


Out[6]:
[1, 2, 'abc', 'def']

In [7]:
l.extend(['ghi',3,4])

In [8]:
l


Out[8]:
[1, 2, 'abc', 'def', 'ghi', 3, 4]

In [9]:
l.append([1,2,3])

In [10]:
l


Out[10]:
[1, 2, 'abc', 'def', 'ghi', 3, 4, [1, 2, 3]]

In [11]:
l.insert(3,'jane')

In [12]:
l


Out[12]:
[1, 2, 'abc', 'jane', 'def', 'ghi', 3, 4, [1, 2, 3]]

In [15]:
p = l.pop()

In [17]:
l


Out[17]:
[1, 2, 'abc', 'jane', 'def', 'ghi', 3]

In [16]:
p


Out[16]:
4

In [18]:
l[2]


Out[18]:
'abc'

In [19]:
l


Out[19]:
[1, 2, 'abc', 'jane', 'def', 'ghi', 3]

In [20]:
l.pop(2)


Out[20]:
'abc'

In [21]:
l


Out[21]:
[1, 2, 'jane', 'def', 'ghi', 3]

In [22]:
l.remove(2)

In [23]:
l


Out[23]:
[1, 'jane', 'def', 'ghi', 3]

In [24]:
l.remove(4)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-b6d11d460bb8> in <module>()
----> 1 l.remove(4)

ValueError: list.remove(x): x not in list

In [25]:
l.pop(2)


Out[25]:
'def'

In [26]:
l.remove()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-7d9f45228ac5> in <module>()
----> 1 l.remove()

TypeError: remove() takes exactly one argument (0 given)

In [27]:
l


Out[27]:
[1, 'jane', 'ghi', 3]

In [28]:
l.reverse()

In [31]:
sorted(l)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-31-5ed0ddfda5a6> in <module>()
----> 1 sorted(l)

TypeError: unorderable types: str() < int()

In [30]:
l.sort()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-30-b56eb6767f72> in <module>()
----> 1 l.sort()

TypeError: unorderable types: str() < int()

In [32]:
l.index(1)


Out[32]:
3

In [33]:
l = [1,2,2,2,3,4]

In [34]:
s = set(l)

In [35]:
s


Out[35]:
{1, 2, 3, 4}

In [38]:
for el in s:
    print(el)


1
2
3
4

In [39]:
s = 1,2,'abc'

In [40]:
type(s)


Out[40]:
tuple

In [41]:
s[1]


Out[41]:
2

In [42]:
student = {'name':'Richard','school':'Columbia'}

In [43]:
student[0]


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-43-ada7f1f882f3> in <module>()
----> 1 student[0]

KeyError: 0

In [44]:
student['name']


Out[44]:
'Richard'

In [45]:
shows = {'firefly':['mal','kaylee','inara'],'battlestar galatica':['starbuck','adama','baltar']}

In [46]:
shows['firefly']


Out[46]:
['mal', 'kaylee', 'inara']

In [47]:
#print out ("in the show <name>, there are the characters <character names>")

In [54]:
for show,characters in shows.items():
    print('In the show',show,'there are the characters',', '.join(characters))


In the show battlestar galatica there are the characters starbuck, adama, baltar
In the show firefly there are the characters mal, kaylee, inara

In [55]:
l = [1,2]

In [56]:
a,b = l

In [57]:
a


Out[57]:
1

In [58]:
b


Out[58]:
2

In [59]:
b, a = l

In [60]:
b


Out[60]:
1

In [61]:
a


Out[61]:
2

In [62]:
l


Out[62]:
[1, 2]

In [63]:
a, b, c = l


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-63-aeca220ab8ae> in <module>()
----> 1 a, b, c = l

ValueError: not enough values to unpack (expected 3, got 2)

In [64]:
l = a,b

In [65]:
l


Out[65]:
(2, 1)

In [66]:
type(l)


Out[66]:
tuple

In [67]:
shows.keys()


Out[67]:
dict_keys(['battlestar galatica', 'firefly'])

In [68]:
shows.values()


Out[68]:
dict_values([['starbuck', 'adama', 'baltar'], ['mal', 'kaylee', 'inara']])

In [69]:
show


Out[69]:
'firefly'

In [70]:
from collections import defaultdict

In [71]:
d = defaultdict(str)

In [72]:
d


Out[72]:
defaultdict(str, {})

In [73]:
d['ak'] = 'alaska'

In [74]:
d


Out[74]:
defaultdict(str, {'ak': 'alaska'})

In [82]:
d['az'] = 'arizona'

In [83]:
d


Out[83]:
defaultdict(str, {'az': 'arizona', 0: '', 'ak': 'alaska'})

In [77]:
d[0]


Out[77]:
''

In [78]:
d


Out[78]:
defaultdict(str, {'az': '', 0: '', 'ak': 'alaska'})

In [79]:
r = {'az':'arizona'}

In [80]:
type(r)


Out[80]:
dict

In [81]:
r['ak']


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-81-aff3cd0ac46a> in <module>()
----> 1 r['ak']

KeyError: 'ak'

In [ ]: