学习一下list等的方法,for while等,function的用法.最多的是list的built-in方法,至于tuple跟dictionary都差不多.另外就是看下for while等循环语句和方法体定义.方法必须定义在调用之前.当然还涉及一些exception等问题.
- #first define a function to show elements in ite
- def show (ite):
- for i in ite:
- print i,
- return ''
- #define a func to sort
- def compare (first, second):
- if first<second:
- return -1;
- elif first==second:
- return 0;
- else:
- return 1;
- _str = ('Hello, ' #say hello to
- 'Python') #to python
- print _str #a long line wrapped string
- #list method
- _list = [89,34,76,21,9,45,107,37,99,34] #initialize a list
- print "Length of _list: %d" % len(_list)
- print "Elements in _list: ", show(_list)
- print "34 exists %d times in _list" % _list.count(34)
- try:
- print "100 was indexed at %d" % _list.index(100)
- except ValueError:
- print "_list has no this value"
- _list.append(100) #append
- print "Length of new _list after append: %d" % len(_list)
- print "Elements in _list: ", show(_list)
- __list = [14,32]
- _list.extend(__list)
- print "Length of new _list after extend: %d" % len(_list)
- print "Elements in _list: ", show(_list)
- _list.insert(20, 78) #beyond the length, not raise exception, just append to the end
- print "Length of new _list after extend: %d" % len(_list)
- print "Elements in _list: ", show(_list)
- try:
- _list.remove(14) #remove
- print "Length of new _list after remove: %d" % len(_list)
- print "Elements in _list: ", show(_list)
- except ValueError:
- print "No 14 in _list"
- _list.reverse()
- print "Elements in _list after reverse: ", show(_list)
- _list_1 = _list[:] #so sort() can't modify _list, but it does when _list_1 = _list
- _list_1.sort()
- print "Elements in _list after default sort: ", show(_list_1)
- _list.sort(compare) #sort([f]), func in python2.3
- print "Elements in _list after self-defined sort: ", show(_list) #same result as sort
- #different between del _list and _list[:]=[]
- del _list_1
- try:
- print "After del _list_1, its length = %d" % len(_list_1)
- except NameError:
- print "After dek _list_1, _list_1 is no longer defined.So len(_list_1) raise a NameError."
- _list[:] = []
- print "After _list[:] = [], Length of _list = %d " % len(_list)
- #some math opration
- print "#some math opration"
- a = 100
- b = 13
- print "a=%d, b=%d" % (a, b)
- print "a/b = %d" % (a/b) #truncate the reminder. result 7
- print "a//b = %f" % (a//b) #also truncate the reminder. result 7.000000
- print "1.*a/b = %f" % (1.*a/b) #result 7.692308
- print "1.*a//b = %f" % (1.*a//b) #always truncate the reminder. result 7.000000
- print "Reminder of a/b = %f" % (a%b) #result 9.000000
- #for statement
- for letter in "Hello python":
- print letter,
- print "" #if print "/n", two line wrapped
- _for_while = "Python hello"
- #while letter in for_while: #has no affect, and can't stop
- # print letter,
- #print "/n"
- _tmp_ite = iter(_for_while) #build a iterator
- while True:
- try:
- print _tmp_ite.next(),
- except StopIteration:
- break;
- #print x,
- print ""
- #range & xrange
- print range(3, 10) #[3-9]
- print xrange(1,5) #just print "xrange(1,5)", not [1,2,3,4]
- for i in xrange(1,10):
- print i,
- print ""
- #function test
- def withDefault (x, y=None):
- if y == None:
- y = []
- y.append(x)
- return y
- print withDefault('huang')
- y = [23]
- print withDefault(99, y)
- print withDefault(104, [45, 'hah'])