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.

2013年6月4日星期二

Class notes for Introduction Python_week 5 and 6

Week 5

1. in mouse click, there should be a position parameter, which is a tuple, unmodifiable.
2. 可以用 list(pos)
3. lst.index(8), return the position of 8 in the list.
4.  82 in lst   returns T, F
5. lst.append(632) adds 632 to the end of the list. one more element added.
6. lst.pop(4) remove the value in position 4;  lst.pop()  remove the last element, and it is the last element..

7 for ball_pos in ball_list.   这里的in 和上面 4 in lst 里面的不一样,这里的是一个接着一个的跑。
8 for kccc in ball_list,  这里面的kccc没有必要是事先定义好的,只要自己心里面认为,并且之后的代码用法一致就好。


9. never remove anything when you in a iteration loop. if you want to remove something, keep track the things and remove it later.

10. start something simple, make it working. modify it, keep going. keep it working....modify it. just make small change.

11. Dictionary: actually a mapping.    Key -> value.   d1= {1:45,‘a’: 55} 定义1是45, a 是55. print d1[1] 返回的是45

12 if index in a dictionary,  just actuall iterate the keys.

13. if something in Python is a constant and never gonna be changed, use the name all capital.

14. def square(number):
          return [n**2 for n in numbers]

15. def balls_in_range2(ball2):
          return [ball for ball in balls if is_in_range(ball)]       14, 15 shows neater way for loop

16. length of a list is 5, but index is from 0, so the max index is 4.

17 dictionary has no order. only un-mutable values as key.

18 d[500] = "wow"   if there is no key 500, it will add key 500 and value wow automatically.
   d[(1,2)] = 10,   map (1,2) to 10

19 for key, value in samplelist.items():   就是成组循环key 和 value
20. samplelist.values() 是仅仅循环value

Week 6

1. Class name should be always capitalized
2. create a field inside an object,  use dot,   e.g.    self.name
3.当object要call 一个class 里面的method时, 就用 objectname.methodname, 但是一定要保证这个object是这个class里面的object。可以用 type(objectname) 来查看。

4.在任何class method 里,第一个参数永远是self,这个会由python自己来管理。

5. class 里面, self 就是object本身。

6. when to create a object in a  class, the first function always __init__

7. in each function in a class, self needs to be a prameter

8. While loop                                 while condition:
                                                            expression

9. in While loop, we do not know when the loop will end, unlike the for loop.