2013年6月16日星期日

Class notes for Introduction Python_week 7 and 8

Week 7

1. do not repeat code again and again. 把重复的code 写成方程或者是class里面的一个方法。如果同样的code重复多次,如果有错,或者需要更改的时候,每一处都要改,太容易出错。

2. 有的时候可以在dictionary里面,call function的name , 而不是 function,然后最后调用的时候在后面加上括号( ), 来调用。
e.g。
def f():
     print "hi"
d = [0: f]
d[0]()

3. 用dictionary来减少重复的code,很好

4. tuple 虽然不能被更改, 但是可以被引用。 比如 tuplename[1]

5.编程时,尽量灵活一些, 尽量避免常数。 并且如果一个常数依赖于另外一个常数,把计算的过程放在code里面,这样,一个变了,另外一个也会自动变。

6. 方程中,return多项的时候, 用逗号分开

7取50%, 50% 的时候 , 可以用 random.randrange(0,2) ==1 来弄。很有意思

week 8

1. sets: no order, no duplicates

2. instructor = set([1,2,3]), set can be iterated
    instructor.add()
   instructor. remove()

3. print 'Rixner' in instructors    ,   return true or false

4. be careful of someset.intersection_update

5. use global variable usually is a bad idea.

没有评论: