https://github.com/parisaalizadeh2003/custom_python_iterator
This project defines a custom Python iterator, Counter, that generates numbers in a specified range with a given step size.
https://github.com/parisaalizadeh2003/custom_python_iterator
iterable iterate iterator loop magicmethod python specialmethod
Last synced: 6 months ago
JSON representation
This project defines a custom Python iterator, Counter, that generates numbers in a specified range with a given step size.
- Host: GitHub
- URL: https://github.com/parisaalizadeh2003/custom_python_iterator
- Owner: ParisaAlizadeh2003
- Created: 2025-03-07T14:31:53.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-03-07T14:37:55.000Z (7 months ago)
- Last Synced: 2025-04-12T00:50:04.434Z (6 months ago)
- Topics: iterable, iterate, iterator, loop, magicmethod, python, specialmethod
- Language: Python
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
---
# **Custom Python Iterator: Counter Class**
## **Overview**
**`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.## **Features**
- Custom iteration with configurable **start**, **end**, and **step** values.
- Generates numbers **one at a time** within the defined range.
## **Usage**```python
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)
```### **Example Output:**
```
0
5
10
15
20
25
30
35
40
45
```## **How to Run**
1. Clone this repository:
```bash
git clone https://github.com/YOUR_USERNAME/Custom_Python_Iterator.git
```2. Navigate to the project directory:
```bash
cd Custom_Python_Iterator
```3. Run the script:
```bash
python counter.py
```## **Contributing**
Feel free to **fork** this repository, **make improvements**, and **submit pull requests**.## **License**
This project is licensed under the **MIT License**.