Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!

Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount

जनरेटर्स


Generators को लागू करना बहुत आसान है, लेकिन समझने में थोड़ा मुश्किल है।

Generators का उपयोग iterator बनाने के लिए किया जाता है, लेकिन एक अलग दृष्टिकोण के साथ। Generators साधारण फंक्शंस होते हैं जो विशेष तरीके से एक बार में एक आइटम के iterable सेट को लौटाते हैं।

जब for स्टेटमेंट का उपयोग करते हुए आइटम्स के सेट पर एक iteration शुरू होता है, तो जनरेटर चलाया जाता है। एक बार जब जनरेटर का फंक्शन कोड "yield" स्टेटमेंट तक पहुंच जाता है, तो जनरेटर अपनी execution को for लूप में वापस दे देता है, सेट से एक नया मान लौटाते हुए। जनरेटर फंक्शन जितने चाहे (संभवतः अनंत) मान उत्पन्न कर सकता है, प्रत्येक को उसके क्रम में yield करते हुए।

यहाँ एक साधारण उदाहरण है एक जनरेटर फंक्शन का जो 7 यादृच्छिक पूर्णांक लौटाता है:

  import random

  def lottery():
      # returns 6 numbers between 1 and 40
      for i in range(6):
          yield random.randint(1, 40)

      # returns a 7th number between 1 and 15
      yield random.randint(1, 15)

  for random_number in lottery():
         print("And the next number is... %d!" %(random_number))

यह फंक्शन यह तय करता है कि यादृच्छिक संख्याएँ कैसे उत्पन्न की जाएँ, और yield स्टेटमेंट्स को एक बार में executes करता है, बीच में, मुख्य for loop को execution वापस देने के लिए रुकते हुए।

Exercise

एक जनरेटर फ़ंक्शन लिखें जो Fibonacci श्रंखला लौटाए। उन्हें नीचे दी गई formula का उपयोग करके गणना की जाती है: श्रंखला के पहले दो नंबर हमेशा 1 के बराबर होते हैं, और प्रत्येक क्रमिक रूप से लौटाया गया नंबर पिछले दो नंबरों का योग होता है। सुझाव: क्या आप जनरेटर फ़ंक्शन में केवल दो चरों का उपयोग कर सकते हैं? याद रखें कि असाइनमेंट को एक साथ किया जा सकता है। कोड

a = 1
b = 2
a, b = b, a
print(a, b)

एक साथ a और b के मानों को स्विच करेगा।

# fill in this function def fib(): pass #this is a null statement which does nothing when executed, useful as a placeholder. # testing code import types if type(fib()) == types.GeneratorType: print("Good, The fib function is a generator.") counter = 0 for n in fib(): print(n) counter += 1 if counter == 10: break # fill in this function def fib(): a, b = 1, 1 while 1: yield a a, b = b, a + b # testing code import types if type(fib()) == types.GeneratorType: print("Good, The fib function is a generator.") counter = 0 for n in fib(): print(n) counter += 1 if counter == 10: break test_output_contains("Good, The fib function is a generator.") success_msg('Good work!')

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Previous Tutorial Next Tutorial Take the Test
Copyright © learnpython.org. Read our Terms of Use and Privacy Policy