Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1817842
  • 博文数量: 276
  • 博客积分: 1574
  • 博客等级: 上尉
  • 技术积分: 2894
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-26 23:23
个人简介

生活的美妙在于,不知道一下秒是惊艳还是伤神,时光流转,珍惜现在的拥有的时光

文章分类

全部博文(276)

文章存档

2017年(17)

2016年(131)

2015年(63)

2013年(2)

2012年(32)

2011年(31)

分类: Python/Ruby

2016-02-16 16:42:36


点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf8 -*-
  3. # Import python libs
  4. from __future__ import print_function

  5. for x in ["spam", "eggs", "ham"]:
  6.     print(x, end='\n')

  7. sum=0
  8. for x in [1,2,3,4]:
  9.     sum +=x
  10. print(sum)
  11. prod=1
  12. for item in [1,2,3,4]:
  13.     prod*=item
  14. print(prod)
  15. #for循环应用于字符串,元组
  16. S='asdfafafdag[4qqqfqf'
  17. T=("a","b","c")
  18. for x in S:
  19.     print(x,end=',')

  20. for y in T:
  21.     print(y,end='_')

  22. Y=((1,2),(3,4),(5,6))
  23. for (a,b) in Y:
  24.     print (a,b)

  25. D={'a':1,'b':2,'c':3}
  26. print(list(D.items()))
  27. L=list(D.items())
  28. print(L)
  29. for (key,value) in D.items():
  30.     print(key, '=>', value)
  31. ((a,b),c) = ((1,2),3)
  32. print(a,b,c)
  33. for ((a,b),c) in [((1,2),3),((4,5),6)]:
  34.     print(a,b,c)
  35. all = (1,2,3,4)
  36. #a,*b,c=(1,2,3,4)
  37. a,b,c=all[0],all[1:3],all[3]
  38. print(a,b,c)


  39. items=['aaa',111,(4,5),2.01]
  40. tests=[(4,5),3.14]
  41. for key in tests:
  42.     for item in items:
  43.         if key == item:
  44.             print(key,'was found.')
  45.             break
  46.     else:
  47.         print(key,'was not found.')
  48. for key in tests:
  49.     if key in items:
  50.         print(key,'was found.')
  51.     else:
  52.         print(key,'was not found.')
  53. seq1='adfklo2'
  54. seq2='aqwerl2'
  55. res=[]
  56. for x in seq1:
  57.     if x in seq2:
  58.         res.append(x)
  59. print(res)
  60. print(range(5))
  61. print(range(2,100,2))
  62. print(range(10,-5,-1))
  63. print(range(-5,5,2))
  64. for i in range(-10,50,2):
  65.     print(i,':line')
  66. X='fghjkl;'
  67. for i in range(len(X)):
  68.     print(X[i],end=',')
  69. for i in X:
  70.     print(i,end='.')
  71. print('================')
  72. Y='asdfghjklwertyuiodfghjkertyu'
  73. for i in range(0,len(Y),3):
  74.     print(Y[i],';')
  75. print(Y[::4])
  76. Z=[1,2,3,4,5]
  77. for i in range(len(Z)):
  78.     Z[i]+=1
  79. print(Z)
  80. A=[10,20,30,40,50]
  81. B=[1,2,3,4]
  82. print(zip(A,B))
  83. for (i,j) in zip(A,B):
  84.     print(i,'+',j,'=',i+j)

  85. C=['ADF','WER','HKL']
  86. D=[1,2,3]
  87. E={}
  88. for k,v in zip(C,D):
  89.     E[k]=v
  90. print(E)
  91. E=dict(zip(C,D))
  92. print(E)
  93. #产生偏移和元素:enumerate
  94. S='asdfghjkl'
  95. for offset,item in enumerate(S):
  96.     print(item,'appears at offset',offset)
  97. '''
  98. a appears at offset 0
  99. s appears at offset 1
  100. d appears at offset 2
  101. f appears at offset 3
  102. g appears at offset 4
  103. h appears at offset 5
  104. j appears at offset 6
  105. k appears at offset 7
  106. l appears at offset 8
  107. '''
  108. #依次取出
  109. E=enumerate(S)
  110. print(next(E))
  111. print(next(E))
  112. print(next(E))
  113. print(next(E))
  114. print(next(E))
  115. print(next(E))
  116. print(next(E))
  117. print(next(E))
  118. print(next(E))
  119. '''
  120. (0, 'a')
  121. (1, 's')
  122. (2, 'd')
  123. (3, 'f')
  124. (4, 'g')
  125. (5, 'h')
  126. (6, 'j')
  127. (7, 'k')
  128. (8, 'l')
  129. '''
  130. #生成与偏移的相对应的个数的元素
  131. print([a * b for a,b in enumerate(S)])
  132. '''
  133. ['', 's', 'dd', 'fff', 'gggg', 'hhhhh', 'jjjjjj', 'kkkkkkk', 'llllllll']
  134. '''


阅读(1168) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~