if n > 1:
return n * fib(n - 1)
else:
print 'end of the line'
return 1
4,内置数据类型List:
List li = ["a", "b", "mpilgrim", "z", "example"]
用“[]”包起来。
A.用for var in list,可以遍历一个list。在遍历的时候不要试着增加和删除元素哦!
squares = [1, 4, 9, 16]
sum = 0
for num in squares:
sum += num
print sum ## 30
B.用in来判断一个元素是否在list中:
list = ['larry', 'curly', 'moe']
if 'curly' in list:
print 'yay
C.list其他的方法:
list.append(elem) -- adds a single element to the end of the list. Common error: does not return the new list, just modifies the original.
list.insert(index, elem) -- inserts the element at the given index, shifting elements to the right.
list.extend(list2) adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().
list.index(elem) -- searches for the given element from the start of the list and returns its index. Throws a ValueError if the element does not appear (use "in" to check without a ValueError).
list.remove(elem) -- searches for the first instance of the given element and removes it (throws ValueError if not present)
list.sort() -- sorts the list in place (does not return it). (The sorted() function shown below is preferred.)
list.reverse() -- reverses the list in place (does not return it)
list.pop(index) -- removes and returns the element at the given index. Returns the rightmost element if index is omitted (roughly the opposite of append()).
D.其他关于list的例子:
list = ['larry', 'curly', 'moe']
list.append('shemp') ## append elem at end
list.insert(0, 'xxx') ## insert elem at index 0
list.extend(['yyy', 'zzz']) ## add list of elems at end
print list ## ['xxx', 'larry', 'curly', 'moe', 'shemp', 'yyy', 'zzz']
print list.index('curly') ## 2
list.remove('curly') ## search and remove that element
list.pop(1) ## removes and returns 'larry'
print list ## ['xxx', 'moe', 'shemp', 'yyy', 'zzz']