Counter
is a Python class that implements a custom iterator. It generates numbers within a specified range, using a given step value. The iterator starts from the defined starting value, increments by the step value, and stops when it reaches or exceeds the end value.
- Custom iteration with configurable start, end, and step values.
- Generates numbers one at a time within the defined range.
class Counter:
def __init__(self, start, end, step=1):
self.start = start
self.end = end
self.step = step
def __iter__(self):
return self
def __next__(self):
if self.start < self.end:
num = self.start
self.start += self.step
return num
raise StopIteration
# Example usage
for i in Counter(0, 50, 5):
print(i)
0
5
10
15
20
25
30
35
40
45
-
Clone this repository:
git clone https://github.com/YOUR_USERNAME/Custom_Python_Iterator.git
-
Navigate to the project directory:
cd Custom_Python_Iterator
-
Run the script:
python counter.py
Feel free to fork this repository, make improvements, and submit pull requests.
This project is licensed under the MIT License.