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 में किया जाता है।

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

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

Custom Iterators

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

Generators

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

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 Electronics
  • 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
computer networks

Table of Contents All Chapters 1. OSI Model      1.1. Physical Layer      1.2. Data Link Layer  ...

computer networks

Table of Contents All Chapters 1. Topology 2. Types of Topology      2.1. Point to Point Topology    ...

computer networks

Table of Contents All Chapters 1. Data Communication 2. Types of Data Communication      2.1. Simplex      2.2....

digital electronics number system

Table of Contents All Chapters 1. Logic Gate 2. AND Gate      2.1. AND Gate Truth Table 3. OR...

Edit Template

Sign up to track your learning

Login to continue your learning tracking

To reset your password, please enter your email address or username below.

Scroll to Top