Python Iterators

Python Iterators

In Python, iterators are objects that implement the iterator protocol, which consists of the __iter__() and __next__() methods. Iterators are used to iterate over a collection of elements or to perform custom iterations in Python.

Here’s an example of a simple iterator that generates a sequence of numbers:

python
class NumberIterator:
    def __init__(self, limit):
        self.limit = limit
        self.current = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.current < self.limit:
            self.current += 1
            return self.current
        else:
            raise StopIteration

# Using the iterator
numbers = NumberIterator(5)
for num in numbers:
    print(num)

In the example above, the NumberIterator class is an iterator that generates numbers from 1 to the specified limit. The __iter__() method returns the iterator object itself, and the __next__() method returns the next element in the sequence. If there are no more elements, it raises the StopIteration exception.

To use the iterator, you can create an instance of the NumberIterator class and iterate over it using a for loop. Each iteration calls the __next__() method to retrieve the next element and prints it.

Note that iterators can also be implemented using generator functions or generator expressions, which provide a more concise and convenient way to create iterators. Here’s an example of an iterator using a generator function:

python
def number_generator(limit):
    current = 0
    while current < limit:
        current += 1
        yield current

# Using the iterator
numbers = number_generator(5)
for num in numbers:
    print(num)

In this case, the number_generator() function is a generator that yields the next number in the sequence. The yield keyword suspends the execution of the function and returns a value, allowing the for loop to iterate over the generated values.

Leave a Reply

Your email address will not be published. Required fields are marked *

GLOTRU Footer
Popular Features
Popular Services/

Website Development & Design

App Development & Design

Graphic Design

Digital Marketing

SEO (Search Engine Optimization)

SMM (Social Media Marketing)

Cyber Security

Company

GLOTRU Founder & CEO : __Azam

Registared : Trade,MSME,etc

Board of Director

Team

About Us

Contact Us

Privacy Policy

Return & Refund Policy

Abuse Policy

Copyright Policy

Cookie Policy

Terms & Conditions

Universal Terms of Service

Disclaimer

Legal

Sponsorships

Investor

Press Releases

Our Investments

Brands

Newsroom

Business

...

_

Digital Millennium Copyright Act
DMCA.com Protection Status

_

Content similarity detection
Protected by Copyscape

_

***ANTI-PIRACY WARNING***

...................................................................................

Follow Us :

...................................................................................

SECURE SERVER : [Legal] [Privacy Policy] [Universal Terms of Service] [Do not sell my personal information]

SITE HOSTED : GLOTRU SECURE SERVER Asian Data Centre [You can host your site][Click Here]

SSL : Server Type : [Cloudflare] Certificate Issued By : [Let's Encrypt] Signature Algorithm : [ECDSA with SHA-384]

SITE BUILD SOFTWARE : Content Management System (CMS) Softwere

_

_

_