जब 'N' एलिमेंट इंक्रीमेंटल टपल्स बनाने की आवश्यकता होती है, तो जेनरेटर एक्सप्रेशन और 'tuple' मेथड का उपयोग किया जा सकता है।
उदाहरण
N = 3
print("The value of 'N' has been initialized")
print("The number of times it has to be repeated is:")
print(N)
my_result = tuple((elem,) * N for elem in range(1, 6))
print("The tuple sequence is:")
print(my_result)
आउटपुट
The value of 'N' has been initialized
The number of times it has to be repeated is: 3
The tuple sequence is: ((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5))
व्याख्या
- 'N' का मान इनिशियलाइज़ किया गया है, और कंसोल पर डिस्प्ले किया गया है।
- एक टपल जिसमें एक विशिष्ट एलिमेंट होता है, दिए गए रेंज में 'N' बार गुणा किया जाता है, एक वेरिएबल को असाइन किया जाता है।
- यह सीक्वेंस जनरेट होता है, और आउटपुट के रूप में कंसोल पर डिस्प्ले किया जाता है।