Home >> Python Tutorial >> Python Iterators
Edit Template
Edit Template

Iterators in Python in Hindi

What is Iterators in Python

Iterators ऐसे objects होता है जो हमे collections (जैसे: lists, tuples और dictionaries) के elements को बिना indexing के एक-एक करके traverse करने देते है, ये iterator protocol को implement करता है, जो __iter__() और __next__() methods से बना होता है।

Key Concepts of Iterators

Iterable

Iterable ऐसे object होते है जो अपने members (elements) को एक-एक करके return करने की capability रखते है (जैसे: lists, tuples, strings, aur dictionaries) इसमे __iter__() method होना चाहिए जो एक iterator return करे।

Iterator

Iterator एक ऐसा object होता है जो data का stream represent करता है, जब __next__() method call होता है तब ये एक-एक करके data return करता है।

Iterator Protocol

एक object को iterator तभी consider किया जाएगा जब उसमे __iter__() और __next__() method हो।

__iter__ Method

__iter__ method iterator object खुद return करता है, ये इसलिए भी जरूरी है ताकि एक iterator object को iterable के रूप में use किया जा सके।

__next__ Method

ये method, sequence (जैसे कोई list, tuple, string, etc.) का next item return करता है, जब end तक पहुँच जाता है तो ये StopIteration exception raise करता है जो signal देता है कि अब sequence में और item नहीं है।

Built-in Iterators

Python कई build-in iterators provide करता है जिसका use lists, tuples, strings और dictionaries में किया जाता है।

🐍
Example 1.py
Copy to clipboard
Copy code
my_tuple = ('a', 'b', 'c')
iterator = iter(my_tuple)

print(next(iterator))  # Output: a
print(next(iterator))  # Output: b
print(next(iterator))  # Output: c
# print(next(iterator))  # Raises StopIteration

iter(): इस function का use एक iterable से iterator return कराने के लिए होता है।

next(): इस function का use iteration से nest item return कराने की लिए होता है।

Custom Iterators

हम custom iterators भी create कर सकते है इसके लिए हमे __iter__() और __next__() method को अपने class में implement करना होगा।

🐍
Example 2.py
Copy to clipboard
class Reverse:
    def __init__(self, data):
        self.data = data
        self.index = len(self.data)

    def __iter__(self):
        return self

    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index -= 1
        return self.data[self.index]

# Custom iterator ka use karte hue
rev = Reverse("Python")
for char in rev:
    print(char)

Generators

Generators, iterators बनाने का एक आसान तरीका होता है जिसमे functions और yield statement का use होता है। ये एक time में सिर्फ एक value ही generate करते है और हर value के बीच अपने state को maintain करते है।

🐍
Example 3.py
Copy to clipboard
def my_generator():
    yield 1
    yield 2
    yield 3

gen = my_generator()
print(next(gen))  # Output: 1
print(next(gen))  # Output: 2
print(next(gen))  # Output: 3
# print(next(gen))  # Raises StopIteration

Function Definition: my_generator function define किया गया है yield statement के साथ।

Yield:  Yield एक value return करता है और next call के लिए अपने state को maintain करके function को pause करता है।

Generator Expressions: Generator expressions, generators को बनाने के लिए compact syntax provide करते है।

उम्मीद करते है कि आपको Python Iterators अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,

Add a comment...

  • All Posts
  • Artificial Intelligence
  • Computer Fundamentals
  • Computer Networks
  • Data Analytics
  • Data Science
  • DBMS
  • Deep Learning
  • Digital Fundamentals
  • DSA with Python
  • Excel
  • Exercise
  • Git & Github
  • Machine Learning
  • Matplotlib
  • Natural Language Processing
  • NumPy
  • Operating System
  • Pandas-s
  • Power BI
  • Python Tutorial
  • Scikit-learn
  • Seaborn
  • SQL & MySQL
File Pointer

Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods      2.1. tell() Method      2.2....

File Handling

Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...

Python Modules

Table of Contents All Chapters 1. Modules in Python 2. Pre-defined Modules      2.1. math      2.2. random...

Python Iterators

Table of Contents All Chapters 1. Iterators      1.1. Iterable      1.2. Iterator      1.3. Iterator Protocol...

Python Polymorphism

Table of Contents All Chapters 1. Polymorphism 2. Key Concepts of Polymorphism 3. Types of Polymorphism      3.1. Polymorphism...

Python Inheritance

Table of Contents All Chapters 1. Inheritance 2. Concepts of Inheritance 3. Types of Inheritance      3.1. Single Inheritance...

Python Function

Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...

Python Dictionary

Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...

Edit Template
Scroll to Top