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

In [2]:
add(5,6)


Out[2]:
11

In [ ]:
#Function name: add

In [5]:
import math
def hyp(a,b):
    c=(a**2) + (b**2)
    x=math.sqrt(c) #math.sqrt(c):return the square root of c
    return x

In [6]:
hyp(3,4)


Out[6]:
5.0

In [7]:
def calc_hypotenuse(a,b):
    return math.sqrt(a**2)+(b**2)

In [8]:
l = [1,2,"acb"]

In [9]:
type(l)


Out[9]:
list

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

In [11]:
l


Out[11]:
[1, 2, 'acb', 'def']

In [ ]:
#append(object)to a list

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

In [13]:
l


Out[13]:
[1, 2, 'acb', 'def', 'ghi', 3, 4]

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

In [15]:
l


Out[15]:
[1, 2, 'acb', 'def', 'ghi', 3, 4, [1, 2, 3]]

In [ ]:
#extend(list):extend original list with elements of specified in list

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

In [20]:
l


Out[20]:
[1, 2, 'acb', 'jane', 'jane', 'def', 'ghi', 3, 4, [1, 2, 3]]

In [ ]:
#inser(index,object)-insert object at specified

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

In [27]:
l


Out[27]:
[1, 2, 'acb', 'jane', 'jane', 'def', 'ghi']

In [28]:
p


Out[28]:
3

In [29]:
l.pop(2) #pop ([index])-pop element from the list at the index


Out[29]:
'acb'

In [ ]:
#remove(value)

In [30]:
l.remove(4)


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

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

In [31]:
l.pop(2)


Out[31]:
'jane'

In [32]:
l.reverse() #reverse

In [33]:
l


Out[33]:
['ghi', 'def', 'jane', 2, 1]

In [34]:
l.index(1)


Out[34]:
4

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

In [36]:
student[0] #the key 0 doesn't exist, that's why there's an error


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

KeyError: 0

In [37]:
student['name']


Out[37]:
'Richard'

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

In [41]:
shows['firefly']


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

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


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

In [ ]:
#.join(list):output: string

In [46]:
from collections import defaultdict

In [47]:
d = defaultdict(str)

In [48]:
d


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

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

In [50]:
d


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

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

In [52]:
d


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

In [53]:
d[0] #0 is an empty string


Out[53]:
''

In [54]:
d


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

In [ ]: