Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Feb 13, 2025
1 parent a35e64a commit d0d8448
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/examples/iterators/range-with-steps/count.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1
2
3
---
2
3
4
5 changes: 5 additions & 0 deletions python/examples/iterators/range-with-steps/count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import it

r = it.Range(1, 6, 2.2)
for n in r:
print(n)
15 changes: 15 additions & 0 deletions python/examples/iterators/range-with-steps/it.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Range():
def __init__(self, start, end, step):
self.current = start
self.end = end
self.step = step

def __iter__(self):
return self

def __next__(self):
if self.current >= self.end:
raise StopIteration
v = self.current
self.current += self.step
return v
8 changes: 8 additions & 0 deletions python/iterators.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ In many aspects it behaves as an iterator. Specifically it allows us to iterate
![](examples/iterators/range.out)


## Range with floating point steps
{id: range-with-floating-point-steps}

![](examples/iterators/range-with-steps/count.py)
![](examples/iterators/range-with-steps/it.py)
![](examples/iterators/range-with-steps/count.out)


## Iterator: a counter
{id: iterator-counter}
{i: StopIteration }
Expand Down

0 comments on commit d0d8448

Please sign in to comment.