इंडेक्स () विधि निर्दिष्ट तत्व के इंडेक्स को टुपल में लौटाती है।
टपल index()
विधि का सिंटैक्स है:
tuple.index (तत्व, प्रारंभ, अंत)
टपल इंडेक्स () पैरामीटर
टपल index()
विधि में अधिकतम तीन तर्क दिए जा सकते हैं:
- तत्व - खोजा जाने वाला तत्व
- प्रारंभ (वैकल्पिक) - इस सूचकांक से खोज शुरू करें
- अंत (वैकल्पिक) - इस सूचकांक तक तत्व को खोजें
टपल इंडेक्स से वापसी मान ()
index()
विधि टपल में दिए गए तत्व के सूचकांक देता है।- यदि तत्व नहीं मिला है, तो एक
ValueError
अपवाद उठाया जाता है।
नोट:index()
विधि केवल मिलान तत्व की पहली आवृत्ति देता है।
उदाहरण 1: तत्व के सूचकांक का पता लगाएं
# vowels tuple vowels = ('a', 'e', 'i', 'o', 'i', 'u') # index of 'e' in vowels index = vowels.index('e') print('The index of e:', index) # element 'i' is searched # index of the first 'i' is returned index = vowels.index('i') print('The index of i:', index)
आउटपुट
E: 1 का सूचकांक e: 2 है
उदाहरण 2: तत्व का सूचकांक टपल में मौजूद नहीं है
# vowels tuple vowels = ('a', 'e', 'i', 'o', 'u') # index of'p' is vowels index = vowels.index('p') print('The index of p:', index)
आउटपुट
ValueError: tuple.index (x): x tuple में नहीं
उदाहरण 3: इंडेक्स का काम () स्टार्ट एंड एंड पैरामीटर्स के साथ
# alphabets tuple alphabets = ('a', 'e', 'i', 'o', 'g', 'l', 'i', 'u') # index of 'i' in alphabets index = alphabets.index('e') # 2 print('The index of e:', index) # 'i' after the 4th index is searched index = alphabets.index('i', 4) # 6 print('The index of i:', index) # 'i' between 3rd and 5th index is searched index = alphabets.index('i', 3, 5) # Error! print('The index of i:', index)
आउटपुट
E: 1 का सूचकांक: I का सूचकांक: 6 ट्रैसबैक (सबसे हालिया कॉल अंतिम): फ़ाइल "", पंक्ति 13, ValueError में: tuple.index (x): x tuple में नहीं