Table of Contents
2. Types of Indexing in Hindi – Positive और Negative Indexing
3. Accessing Elements Using Indexes in Hindi – Index से Elements Access करना
4. Difference Between Indexing and Slicing in Hindi – इंडेक्सिंग और स्लाइसिंग में फर्क
6. Disadvantages of Indexing in Hindi – इंडेक्सिंग की सीमाएं
Indexing in Python in Hindi – इंडेक्सिंग क्या है?
Indexing Python में किसी sequence (जैसे string, list या tuple) के एक particular element को उसकी position के through access करने का तरीका है।
आसान भाषा में कहें तो, हर element की एक fixed position यानी index होती है, और उस index के through हम सीधे उस element तक पहुँच सकते हैं।
Python में indexing हमेशा 0 से शुरू होती है, यानी sequence का पहला element index 0 पर होता है, दूसरा element index 1 पर होता है, और इसी तरह आगे बढ़ता है।
Indexing का इस्तेमाल strings, lists, tuples जैसे सभी ordered data types में किया जा सकता है, लेकिन dictionary जैसे unordered data types पर सीधे index से access नहीं किया जा सकता।
किसी भी element को access करने के लिए square brackets [ ] के अंदर उसकी index value लिखी जाती है।
Indexing की मदद से किसी बड़े data structure में से भी किसी specific element को बहुत तेजी से निकाला जा सकता है, बिना पूरे data को loop करने के।
अगर दी गई index sequence की length से बाहर हो, तो Python "IndexError" नाम का error देता है, इसलिए index हमेशा valid range के अंदर होनी चाहिए।
Indexing को समझना Python सीखने का सबसे शुरुआती और सबसे जरूरी step है, क्योंकि यह strings, lists, tuples सबकी base है।
Example of Indexing – एक Simple शुरुआत
मान लो आपके पास fruits के नामों की एक list है और आपको सिर्फ दूसरा fruit निकालना है। इसके लिए indexing सबसे आसान तरीका है।
fruits = ["apple", "banana", "mango", "orange"]
print(fruits[1])
# Output: banana
यहाँ index 1 को इस्तेमाल किया गया, क्योंकि Python में counting हमेशा 0 से शुरू होती है, इसलिए "banana" दूसरा element होते हुए भी index 1 पर है।
Types of Indexing in Hindi – Positive और Negative Indexing
1. Positive Indexing
Positive indexing में counting शुरुआत यानी left side से होती है, और पहला element हमेशा index 0 पर होता है। यह सबसे common और basic तरीका है elements को access करने का।
numbers = [10, 20, 30, 40, 50]
print(numbers[0]) # Output: 10
print(numbers[2]) # Output: 30
print(numbers[4]) # Output: 50
2. Negative Indexing
Negative indexing में counting अंत यानी right side से शुरू होती है, और आखिरी element की index -1 होती है। यह तब बहुत काम आता है जब हमें sequence के आखिरी elements चाहिए हों, बिना उसकी length पता किए।
numbers = [10, 20, 30, 40, 50]
print(numbers[-1]) # Output: 50
print(numbers[-2]) # Output: 40
print(numbers[-5]) # Output: 10
3. Strings में Indexing का Example
Indexing सिर्फ lists तक सीमित नहीं है, strings के हर character पर भी positive और negative दोनों तरह की indexing लगाई जा सकती है।
word = "PYTHON"
print(word[0]) # Output: P
print(word[-1]) # Output: N
यह SVG "PYTHON" शब्द के हर character की positive और negative index एक साथ दिखा रहा है।
Accessing Elements Using Indexes in Hindi – Index से Elements Access करना
1. List में Index से Access करना
List के किसी भी element को उसकी index square brackets में लिखकर access किया जाता है।
colors = ["red", "green", "blue", "yellow"]
print(colors[2])
# Output: blue
2. Tuple में Index से Access करना
Tuple भी list की तरह ही ordered होती है, इसलिए उसमें भी वही indexing rule apply होता है, बस tuple के elements को बदला नहीं जा सकता।
coordinates = (10, 20, 30)
print(coordinates[0])
# Output: 10
3. Index से Value Update करना (सिर्फ List में)
चूंकि list mutable होती है, इसलिए किसी particular index पर मौजूद value को सीधे नई value से replace किया जा सकता है।
marks = [80, 90, 70]
marks[1] = 95
print(marks)
# Output: [80, 95, 70]
4. IndexError को समझना
अगर दी गई index range से बाहर हो, तो Python IndexError देता है। इसलिए हमेशा valid index range के अंदर ही काम करना चाहिए।
numbers = [1, 2, 3]
print(numbers[5])
# Output: IndexError: list index out of range
5. Nested List में Indexing करना
अगर list के अंदर एक और list हो, तो उसे access करने के लिए एक के बाद एक index लगानी पड़ती है।
matrix = [[1, 2, 3], [4, 5, 6]]
print(matrix[1][2])
# Output: 6
Difference Between Indexing and Slicing in Hindi – इंडेक्सिंग और स्लाइसिंग में फर्क
| Point | Indexing | Slicing |
|---|---|---|
| Kaam (Function) | Sequence का सिर्फ एक single element access करती है | Sequence में से एक range या हिस्सा (sub-sequence) निकालती है |
| Syntax | sequence[index] | sequence[start:stop:step] |
| Return Type | Single value (जैसे string का एक character या list का एक element) | पूरा sub-sequence, यानी नई list, string या tuple |
| Example | fruits[1] → सिर्फ एक fruit return करेगा | fruits[1:3] → कई fruits की एक नई list return करेगा |
| Out of Range Error | Index range से बाहर होने पर IndexError आता है | Range से बाहर जाने पर भी error नहीं आता, बस result छोटा या empty मिलता है |
| Use Case | जब सिर्फ एक specific element चाहिए हो | जब continuous elements का कोई हिस्सा चाहिए हो |
Indexing और Slicing को Example से समझें
fruits = ["apple", "banana", "mango", "orange", "grapes"]
print(fruits[1])
# Indexing Output: banana
print(fruits[1:4])
# Slicing Output: ['banana', 'mango', 'orange']
ऊपर के example में साफ दिख रहा है कि indexing सिर्फ एक single element return करती है, जबकि slicing उसी list में से कई elements की एक नई list बना देती है।
यह SVG indexing और slicing के बीच के मुख्य फर्क को दिखा रहा है।
Importance of Indexing in Hindi
1. Fast Data Access
Indexing की मदद से किसी भी बड़े data structure से भी एक particular element सीधे और तेजी से access किया जा सकता है।
2. दूसरे Concepts की Base
Slicing, looping और string manipulation जैसे बहुत से concepts indexing की समझ पर ही टिके होते हैं, इसलिए इसे अच्छे से सीखना जरूरी है।
3. Data को Modify करने में मदद
Lists जैसी mutable data types में किसी particular index पर मौजूद value को बदलने के लिए indexing का इस्तेमाल किया जाता है।
Advantages of Indexing in Hindi – इंडेक्सिंग के फायदे
किसी भी specific element को बहुत जल्दी access किया जा सकता है।
Positive और negative दोनों तरह से elements को access करने की flexibility मिलती है।
Lists जैसी mutable data types में values को update करना आसान हो जाता है।
Strings, lists और tuples सभी में एक जैसा concept काम करता है, इसलिए सीखना आसान है।
Disadvantages of Indexing in Hindi – इंडेक्सिंग की सीमाएं
Range से बाहर index देने पर IndexError आ जाता है।
Dictionary जैसी unordered data types पर सीधे numeric index से access नहीं किया जा सकता।
Beginners अक्सर 0 से शुरू होने वाली counting को लेकर confuse हो जाते हैं।
Negative indexing को समझने में शुरुआत में थोड़ी दिक्कत हो सकती है।