Guest User

Untitled

a guest
Jul 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. print("***** FOR LOOP *****")
  2. for i in range(5):
  3. print(i)
  4.  
  5. primes = [2, 3, 5, 7]
  6. for i in range(len(primes)):
  7. print("index %d " % primes[i])
  8.  
  9. # ---------------- OUTPUT ----------------
  10. # ***** For loop *****
  11. # 0
  12. # 1
  13. # 2
  14. # 3
  15. # 4
  16. # index 2
  17. # index 3
  18. # index 5
  19. # index 7
  20.  
  21.  
  22. print("***** FOR LOOP USING STRING *****")
  23. hello_world = "Hello, World!"
  24.  
  25. # print each character
  26. for ch in hello_world:
  27. print(ch);
  28.  
  29. # count how many character using for loop
  30. length = 0
  31. for ch in hello_world:
  32. length += 1;
  33. print("number of characters %d " % length)
  34.  
  35. # ---------------- OUTPUT ----------------
  36. # ***** FOR LOOP USING STRING *****
  37. # H
  38. # e
  39. # l
  40. # l
  41. # o
  42. # ,
  43. #
  44. # W
  45. # o
  46. # r
  47. # l
  48. # d
  49. # !
  50. # number of characters 13
  51.  
  52.  
  53. print("***** WHILE LOOP *****")
  54. square = 1
  55.  
  56. # print 1 to 10
  57. while square <= 10:
  58. print(square)
  59. square += 1
  60.  
  61. # print square 1 to 100 with power ** (1, 4, 16...)
  62. number = 1
  63. while square <= 99:
  64. square = number ** 2
  65. print("square %d" % square)
  66. number += 1
  67.  
  68.  
  69. # ---------------- OUTPUT ----------------
  70. # ***** WHILE LOOP *****
  71. # 1
  72. # 2
  73. # 3
  74. # 4
  75. # 5
  76. # 6
  77. # 7
  78. # 8
  79. # 9
  80. # 10
  81. # square 1
  82. # square 4
  83. # square 9
  84. # square 16
  85. # square 25
  86. # square 36
  87. # square 49
  88. # square 64
  89. # square 81
  90. # square 100
  91.  
  92.  
  93. print("***** BREAK *****")
  94. count = 0
  95.  
  96. # logically this cannot be False
  97. while True:
  98. print(count)
  99. count += 1
  100. if count >= 5:
  101. # exit loop
  102. break
  103.  
  104. zoo = ["lion", 'tiger', 'elephant']
  105.  
  106. # logically this cannot be False
  107. while True:
  108. # pop: return last item in the list
  109. animal = zoo.pop()
  110. print(animal)
  111. if animal == "lion":
  112. break;
  113.  
  114. # ---------------- OUTPUT ----------------
  115. # ***** BREAK *****
  116. # 0
  117. # 1
  118. # 2
  119. # 3
  120. # 4
  121. # elephant
  122. # tiger
  123. # lion
  124.  
  125.  
  126. print("***** CONTINUE *****")
  127. for i in range(5):
  128. if i == 3:
  129. # skip the rest of the code and move to the next loop
  130. continue
  131. print(i)
  132.  
  133. # print only odd numbers
  134. for x in range(10):
  135. if (x % 2) == 0:
  136. # skip print(x) for this loop
  137. continue
  138. print("event number %d" % x)
  139.  
  140. # ---------------- OUTPUT ----------------
  141. # ***** CONTINUE *****
  142. # 0
  143. # 1
  144. # 2
  145. # 4
  146. # event number 1
  147. # event number 3
  148. # event number 5
  149. # event number 7
  150. # event number 9
Add Comment
Please, Sign In to add comment