In [1]:
def add(x,y):
return x+y
In [2]:
add(5,6)
Out[2]:
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]:
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]:
In [10]:
l.append('def')
In [11]:
l
Out[11]:
In [ ]:
#append(object)to a list
In [12]:
l.extend(['ghi',3,4])
In [13]:
l
Out[13]:
In [14]:
l.append([1,2,3])
In [15]:
l
Out[15]:
In [ ]:
#extend(list):extend original list with elements of specified in list
In [19]:
l.insert(3,'jane')
In [20]:
l
Out[20]:
In [ ]:
#inser(index,object)-insert object at specified
In [26]:
p = l.pop()
In [27]:
l
Out[27]:
In [28]:
p
Out[28]:
In [29]:
l.pop(2) #pop ([index])-pop element from the list at the index
Out[29]:
In [ ]:
#remove(value)
In [30]:
l.remove(4)
In [31]:
l.pop(2)
Out[31]:
In [32]:
l.reverse() #reverse
In [33]:
l
Out[33]:
In [34]:
l.index(1)
Out[34]:
In [35]:
student={'name':'Richard','school':'Columbia'}
In [36]:
student[0] #the key 0 doesn't exist, that's why there's an error
In [37]:
student['name']
Out[37]:
In [40]:
shows={'firefly':['mal','kaylee','inara'],'battlestar galatica':['starbuck']}
In [41]:
shows['firefly']
Out[41]:
In [45]:
for show,characters in shows.items():
print ('In the show', show,'there are the characters',','.join(characters))
In [ ]:
#.join(list):output: string
In [46]:
from collections import defaultdict
In [47]:
d = defaultdict(str)
In [48]:
d
Out[48]:
In [49]:
d['ak']='alaska'
In [50]:
d
Out[50]:
In [51]:
d['az']='arizona'
In [52]:
d
Out[52]:
In [53]:
d[0] #0 is an empty string
Out[53]:
In [54]:
d
Out[54]:
In [ ]: