[关闭]
@linux1s1s 2017-08-09T10:31:15.000000Z 字数 1239 阅读 585

Python 核心编程笔记二

Python 2017-08


这里简要记录 Python核心编程 读书笔记 整段代码 可以直接运行

CODE

  1. # -*- coding:gb18030 -*-
  2. # 序列
  3. s = 'abcdefgh'
  4. # 顺序取出字符
  5. print s[::1]
  6. # 倒序取出字符
  7. print s[::-1]
  8. # 每隔一个取出一个字符
  9. print s[::2]
  10. # 内置函数,将首字母大写
  11. print s.capitalize()
  12. st = ''' hi 234592034860!@#$%^&*********()
  13. there'''
  14. print st
  15. # 验证元组的Immutable(其和Java中String一样具有Immutable)
  16. t = ('a', 'b')
  17. print t
  18. print id(t)
  19. t += ('c', 'd')
  20. print t
  21. print id(t)
  22. print
  23. # 元组(tuple)的表示,只有一个元素的元组一般要用,作为后缀
  24. x = ('abc')
  25. print type(x)
  26. print x
  27. y = ('abc',)
  28. print type(y)
  29. print y
  30. def showMaxFactor(num):
  31. count = num/2
  32. while count > 1:
  33. if num % 2 == 0:
  34. print 'larget factor of %d is %d' %(num, count)
  35. break
  36. count -= 1
  37. else:
  38. print num, 'is prime'
  39. for eachNum in range(10,21):
  40. showMaxFactor(eachNum)
  41. print
  42. # 元组,迭代器
  43. myTuple = (123, 'xyz', 45.67)
  44. i = iter(myTuple)
  45. print i.next()
  46. print i.next()
  47. print i.next()
  48. # lambda表达式
  49. x = map(lambda x: x ** 2 , range(6))
  50. print x
  51. # 列表解析式
  52. z = [x ** 2 for x in range(6)]
  53. print z
  54. # 生成器解析
  55. j = (x ** 2 for x in range(7))
  56. print

RESULT

  1. C:\Python27\python.exe H:/workspace/python-hw/hw-2.py
  2. abcdefgh
  3. hgfedcba
  4. aceg
  5. Abcdefgh
  6. hi 234592034860!@#$%^&*********()
  7. there
  8. ('a', 'b')
  9. 36770056
  10. ('a', 'b', 'c', 'd')
  11. 36097384
  12. <type 'str'>
  13. abc
  14. <type 'tuple'>
  15. ('abc',)
  16. larget factor of 10 is 5
  17. 11 is prime
  18. larget factor of 12 is 6
  19. 13 is prime
  20. larget factor of 14 is 7
  21. 15 is prime
  22. larget factor of 16 is 8
  23. 17 is prime
  24. larget factor of 18 is 9
  25. 19 is prime
  26. larget factor of 20 is 10
  27. 123
  28. xyz
  29. 45.67
  30. [0, 1, 4, 9, 16, 25]
  31. [0, 1, 4, 9, 16, 25]
  32. Process finished with exit code 0
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注