Advertisement
asweigart

Untitled

May 30th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1.  
  2. '''
  3. for i in '01234':
  4. print(i)
  5.  
  6.  
  7. the_iteratable = '01234'
  8. the_iterator = iter(the_iteratable)
  9. while True:
  10. try:
  11. i = next(the_iterator)
  12. except StopIteration:
  13. break
  14. print(i)
  15. '''
  16. # range(2, 10, 2)
  17. def my_range(start, stop, step):
  18. current = start
  19. while current < stop:
  20. yield current
  21. current += step
  22.  
  23. '''
  24. gen_obj = iter(my_range(2, 10, 2))
  25. while True:
  26. try:
  27. print(next(gen_obj))
  28. except StopIteration:
  29. break
  30. '''
  31. '''
  32. for i in my_range(2, 10, 2):
  33. print(i)
  34. '''
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. al@jfish python % python3
  52. Python 3.13.3 (main, Apr 8 2025, 13:54:08) [Clang 16.0.0 (clang-1600.0.26.6)] on darwin
  53. Type "help", "copyright", "credits" or "license" for more information.
  54. >>> for i in range(5):
  55. ... print(i)
  56. ...
  57. 0
  58. 1
  59. 2
  60. 3
  61. 4
  62. >>> range(5)
  63. range(0, 5)
  64. >>> type(range(5))
  65. <class 'range'>
  66. >>> r1 = range(5)
  67. >>> r2 = range(0, 10, 2)
  68. >>> next(r1)
  69. Traceback (most recent call last):
  70. File "<python-input-5>", line 1, in <module>
  71. next(r1)
  72. ~~~~^^^^
  73. TypeError: 'range' object is not an iterator
  74. >>> r1 = iter(r1)
  75. >>> r2 = iter(r2)
  76. >>>
  77. >>>
  78. >>>
  79. >>> next(r1)
  80. 0
  81. >>> next(r1)
  82. 1
  83. >>> next(r1)
  84. 2
  85. >>> next(r1)
  86. 3
  87. >>> next(r1)
  88. 4
  89. >>> next(r1)
  90. Traceback (most recent call last):
  91. File "<python-input-16>", line 1, in <module>
  92. next(r1)
  93. ~~~~^^^^
  94. StopIteration
  95. >>> range_obj = range(0, 10, 2)
  96. >>> iterator1 = iter(range_obj)
  97. >>> iterator2 = iter(range_obj)
  98. >>> next(iterator1)
  99. 0
  100. >>> next(iterator1)
  101. 2
  102. >>> next(iterator1)
  103. 4
  104. >>> next(iterator2)
  105. 0
  106. >>> next(iterator2)
  107. 2
  108. >>> next(iterator2)
  109. 4
  110. >>> # An iterable obj (like a range obj, string, list, dict.keys(), set obj) is passed to iter() whic\
  111. h returns an iterator obj.
  112. >>> i3 = iter(iterator1)
  113. >>> i3 == iterator1)
  114. File "<python-input-28>", line 1
  115. i3 == iterator1)
  116. ^
  117. SyntaxError: unmatched ')'
  118. >>> i3 == iterator1
  119. True
  120. >>> next(i3)
  121. 6
  122. >>> next(iterator2)
  123. 6
  124. >>> next(iterator2)
  125. 8
  126. >>> next(iterator2)
  127. Traceback (most recent call last):
  128. File "<python-input-33>", line 1, in <module>
  129. next(iterator2)
  130. ~~~~^^^^^^^^^^^
  131. StopIteration
  132. >>> next(iterator2)
  133. Traceback (most recent call last):
  134. File "<python-input-34>", line 1, in <module>
  135. next(iterator2)
  136. ~~~~^^^^^^^^^^^
  137. StopIteration
  138. >>> next(iterator2)
  139. Traceback (most recent call last):
  140. File "<python-input-35>", line 1, in <module>
  141. next(iterator2)
  142. ~~~~^^^^^^^^^^^
  143. StopIteration
  144. >>>
  145. >>>
  146. >>>
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement