实验一下Python中初级的方法

再次好好看看Python,看看里边初级的方法如print等,以及三个内建类型list,tuple,dictionary,以及内建方法slice等的操作,虽然都是很基础的东西,但是记录下来,以后可以复习巩固.
  1. _str = "Hello, Python!"
  2. print "_str's length = ", len(_str)
  3. _list = [123]
  4. print "_list's length = ", len(_list)
  5. _tuple = tuple(_str)
  6. print "_tuple's length = ", len(_tuple)
  7. #max() & min()
  8. print "Max(_list) = ", max(_list)   #result:3
  9. print "Min(_list) = ", min(_list)   #result:1
  10. #slice a list
  11. _list_slice = _list[1:3]
  12. print "Members in _list_slice:";
  13. for _ls in _list_slice: #result:2 3.Begin form index 1, end before index 3
  14.     print _ls
  15. #modify a list using 'slice'
  16. _list[1:4] = [6410]
  17. print "Members in new list:";
  18. for _ls in _list: #result:1 6 4 10.Length changed to 4
  19.     print _ls
  20. #get tuple elements
  21. print "Members in _tuple:";
  22. for _t in _tuple:   #result:Hello, Python!(over).Each char a line.
  23.     print _t
  24. #slice a tuple
  25. _tuple_slice = _tuple[:-1]
  26. print "Members in _tuple_slice:";
  27. for _ts in _tuple_slice:    #result: H e l l o , [space] P y t h o n(over).End before the last one.All char in a line as a result of '//'.(maybe ",")
  28.     print _ts, /
  29. #must go on in the next line, otherwise a "invalid syntax" error. comment or null line is help.
  30. #however, if no "/" last line, just end with ",", we can use "print" just after last line, with content in the same line if no "/n"
  31. print "/nhello"
  32. #dictionary
  33. _dic = {    'i' : 'u',
  34.             'he' : 'she'}
  35. _dic['him'] = 'her'     #add an element him->her
  36. _dic[9] = 'I am'        #add an element 9->I am
  37. print "_dic's length = " , len(_dic)
  38. for _key in _dic:   #result: as in _dic, not as the order
  39.     print _key, "/t:/t", _dic[_key]
  40. #usage of "print"
  41. print "It's %d bug, maybe!" % 1                 #result: it's 1 bug, maybe!
  42. print ("not like %s and %s" % ('C''C++'))     #result: not like c and c++
  43. #modify tuple
  44. try:
  45.     _tuple[1] = 'r'     #raise an error
  46. except TypeError:
  47.     print "Tuple object can't be modified"

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值