学习一下list等的方法,for while等,function的用法

学习一下list等的方法,for while等,function的用法.最多的是list的built-in方法,至于tuple跟dictionary都差不多.另外就是看下for  while等循环语句和方法体定义.方法必须定义在调用之前.当然还涉及一些exception等问题.
  1. #first define a function to show elements in ite
  2. def show (ite):
  3.     for i in ite:
  4.         print i,
  5.     return ''
  6. #define a func to sort
  7. def compare (first, second):
  8.     if first<second:
  9.         return -1;
  10.     elif first==second:
  11.         return 0;
  12.     else:
  13.         return 1;
  14.     
  15. _str = ('Hello, '   #say hello to
  16.         'Python')   #to python
  17. print _str          #a long line wrapped string
  18. #list method
  19. _list = [89,34,76,21,9,45,107,37,99,34]    #initialize a list
  20. print "Length of _list: %d" % len(_list)
  21. print "Elements in _list: ", show(_list)
  22. print "34 exists %d times in _list" % _list.count(34)
  23. try:
  24.     print "100 was indexed at %d" % _list.index(100)
  25. except ValueError:
  26.     print "_list has no this value"
  27. _list.append(100)   #append
  28. print "Length of new _list after append: %d" % len(_list)
  29. print "Elements in _list: ", show(_list)
  30. __list = [14,32]
  31. _list.extend(__list)
  32. print "Length of new _list after extend: %d" % len(_list)
  33. print "Elements in _list: ", show(_list)
  34. _list.insert(2078)    #beyond the length, not raise exception, just append to the end
  35. print "Length of new _list after extend: %d" % len(_list)
  36. print "Elements in _list: ", show(_list)
  37. try:
  38.     _list.remove(14)    #remove
  39.     print "Length of new _list after remove: %d" % len(_list)
  40.     print "Elements in _list: ", show(_list)
  41. except ValueError:
  42.     print "No 14 in _list"
  43. _list.reverse()
  44. print "Elements in _list after reverse: ", show(_list)
  45. _list_1 = _list[:]      #so sort() can't modify _list, but it does when _list_1 = _list
  46. _list_1.sort()
  47. print "Elements in _list after default sort: ", show(_list_1)
  48. _list.sort(compare)     #sort([f]), func in python2.3
  49. print "Elements in _list after self-defined sort: ", show(_list)    #same result as sort
  50. #different between del _list and _list[:]=[]
  51. del _list_1
  52. try:
  53.     print "After del _list_1, its length = %d" % len(_list_1)
  54. except NameError:
  55.     print "After dek _list_1, _list_1 is no longer defined.So len(_list_1) raise a NameError."
  56. _list[:] = []
  57. print "After _list[:] = [], Length of _list = %d " % len(_list)
  58. #some math opration
  59. print "#some math opration"
  60. a = 100
  61. b = 13
  62. print "a=%d, b=%d" % (a, b)
  63. print "a/b = %d" % (a/b)    #truncate the reminder. result 7
  64. print "a//b = %f" % (a//b)  #also truncate the reminder. result 7.000000
  65. print "1.*a/b = %f" % (1.*a/b) #result 7.692308
  66. print "1.*a//b = %f" % (1.*a//b)    #always truncate the reminder. result 7.000000
  67. print "Reminder of a/b = %f" % (a%b)    #result 9.000000
  68. #for statement
  69. for letter in "Hello python":
  70.     print letter,
  71. print ""        #if print "/n", two line wrapped
  72. _for_while = "Python hello"
  73. #while letter in for_while: #has no affect, and can't stop
  74. #    print letter,
  75. #print "/n"
  76. _tmp_ite = iter(_for_while) #build a iterator
  77. while True:
  78.     try:
  79.         print _tmp_ite.next(),
  80.     except StopIteration:
  81.         break;
  82.     #print x,
  83. print ""
  84. #range & xrange
  85. print range(310)  #[3-9]
  86. print xrange(1,5)   #just print "xrange(1,5)", not [1,2,3,4]
  87. for i in xrange(1,10):
  88.     print i,
  89. print ""
  90. #function test
  91. def withDefault (x, y=None):
  92.     if y == None:
  93.         y = []
  94.     y.append(x)
  95.     return y
  96. print withDefault('huang')
  97. y = [23]
  98. print withDefault(99, y)
  99. print withDefault(104, [45'hah'])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值