Chinaunix首页 | 论坛 | 博客
  • 博客访问: 436895
  • 博文数量: 89
  • 博客积分: 2713
  • 博客等级: 少校
  • 技术积分: 938
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-18 21:19
个人简介

为了成为自由自在的人而奋斗!

文章分类

全部博文(89)

文章存档

2016年(5)

2015年(9)

2014年(2)

2013年(10)

2012年(1)

2011年(30)

2010年(32)

分类: Python/Ruby

2013-09-23 23:41:59

模块引用, 一个py文件可以做一个模块, 通过__name__控制主块是否运行

点击(此处)折叠或打开

  1. if __name__ == '__main__':
  2.     print 'This program is being run by itself'
  3. else:
  4.     print 'I am being imported from another module'
dir函数 列举模块的标识符(变量、函数、类等)



数据结构:
list使用中括号包含,逗号分隔    a = [1, 2, 3, 4, 5]  可以改变
元组使用圆括号包含,逗号分隔 b = (1, 2,3, 4)  不可改变


list的操作

点击(此处)折叠或打开

  1. #!C:\Python33
  2. shoplist = ['apple', 'mango', 'carrot', 'banana']

  3. for item in shoplist:
  4.     print (item)

  5. shoplist.append('rice')

  6. print('================')
  7. for item in shoplist:
  8.     print (item)

  9. print('================')
  10. shoplist.sort()
  11. for item in shoplist:
  12.     print (item)

  13. print('================')
  14. del shoplist[1:3]
  15. for item in shoplist:
  16.     print (item)

  17. 结果

  18. >>>
  19. apple
  20. mango
  21. carrot
  22. banana
  23. ================
  24. apple
  25. mango
  26. carrot
  27. banana
  28. rice
  29. ================
  30. apple
  31. banana
  32. carrot
  33. mango
  34. rice
  35. ================
  36. apple
  37. mango
  38. rice
  39. >>>
元组输出:

点击(此处)折叠或打开

  1. age = 22
  2. name = 'name'

  3. print ('%s is %d years old' % (name, age))
  4. print ('Why is %s playing with that python?' % name )

  5. 结果:


  6. >>>
  7. name is 22 years old
  8. Why is name playing with that python?
  9. >>>

  10. 注意 格式化输出 中间没有逗号
字典 键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。

点击(此处)折叠或打开

  1. #!C:\Python33
  2. a = {1:'one', 2:'two'}

  3. print('=======================')

  4. for i,st in a.items():
  5.     print(" %d : %s" %(i, st))


  6. print('=======================')

  7. a[3] = 'three'
  8. a[4] = 'four'
  9. for i,st in a.items():
  10.     print(" %d : %s" %(i, st))

  11. print('=======================')

  12. del a[3]
  13. for i,st in a.items():
  14.     print(" %d : %s" %(i, st))

  15. 结果:
  16. =======================
  17.  1 : one
  18.  2 : two
  19. =======================
  20.  1 : one
  21.  2 : two
  22.  3 : three
  23.  4 : four
  24. =======================
  25.  1 : one
  26.  2 : two
  27.  4 : four
  28. >>>



参考 -- 列表,元组, 如果直接赋值则使用同一个对象, 否则需要使用切片操作获取新对象


点击(此处)折叠或打开

  1. #!C:\Python33
  2. a = ['one', 'two', 'three', 'fourth']
  3. b = a
  4. c = a[1:2]
  5. del a[1]

  6. print('=========b==============')

  7. for i in b:
  8.     print(i)

  9. print('==========c=============')

  10. for st in c:
  11.     print("%s" %st)
  12. 结果:
  13. >>>
  14. =========b==============
  15. one
  16. three
  17. fourth
  18. ==========c=============
  19. two
  20. >>>


阅读(1455) | 评论(0) | 转发(1) |
0

上一篇:pythons基础学习-3

下一篇:python基础学习-5

给主人留下些什么吧!~~