Slicing in Tuple :-
Python में Slicing in Tuple का अर्थ है Tuple में से एक से अधिक elements के किसी particular part (भाग) या range को access करना। Slicing की सहायता से हम Tuple के elements की एक specific range को आसानी से प्राप्त कर सकते हैं।
Tuple में slicing के लिए square brackets [ ] का उपयोग किया जाता है। इसमें start index शामिल होता है, लेकिन stop index शामिल नहीं होता।
English
Slicing in Tuple means accessing a particular portion or range of elements from a tuple. Slicing allows us to access multiple elements of a tuple at once.
The start index is included, but the stop index is excluded.
Syntax:-
tuple[start:stop]
Example:-
numbers = (10, 20, 30, 40, 50)
print(numbers[1:4])
Output:-
(20, 30, 40)
Because:-
Tuple के elements और indexes:
Element : 10 20 30 40 50
Index : 0 1 2 3 4
numbers[1:4] में:
Index 1 → 20 शामिल होगा।
Index 2 → 30 शामिल होगा।
Index 3 → 40 शामिल होगा।
Index 4 → include नहीं होगा।
इसलिए output:
(20, 30, 40)
English
Here, the slicing starts from index 1 and stops before index 4. Therefore, elements at indexes 1, 2, and 3 are returned.
Slicing with Start Index :-
यदि केवल start index दिया जाए, तो Tuple start index से end तक के elements return करता है।
numbers = (10, 20, 30, 40, 50)
print(numbers[2:])
Output:-
(30, 40, 50)
यहाँ index 2 से last element तक के elements प्राप्त होते हैं।
English
When only the start index is specified, slicing returns elements from the start index to the end of the tuple.
Slicing with Stop Index :-
यदि start index नहीं दिया जाए, तो slicing Tuple की शुरुआत से दिए गए stop index तक होती है।
numbers = (10, 20, 30, 40, 50)
print(numbers[:3])
Output:-
(10, 20, 30)
यहाँ index 3 शामिल नहीं होता।
Slicing with Step :-
Slicing में step का उपयोग यह निर्धारित करने के लिए किया जाता है कि elements के बीच कितना gap रखा जाए।
Syntax:-
tuple[start:stop:step]
Example:-
numbers = (10, 20, 30, 40, 50, 60)
print(numbers[0:6:2])
Output:-
(10, 30, 50)
Because:-
यहाँ step = 2 है, इसलिए हर दूसरा element लिया गया।
English
Here, the step value is 2, so every second element is selected.
Negative Indexing with Slicing :-
Tuple slicing में negative indexes का भी उपयोग किया जा सकता है।
numbers = (10, 20, 30, 40, 50)
print(numbers[-4:-1])
Output:-
(20, 30, 40)
Because:-
Negative indexes:
-5 → 10
-4 → 20
-3 → 30
-2 → 40
-1 → 50
numbers[-4:-1] में -1 stop index है, इसलिए 50 शामिल नहीं होगा।
Reverse Slicing :-
Negative step का उपयोग करके Tuple को reverse order में access किया जा सकता है।
numbers = (10, 20, 30, 40, 50)
print(numbers[::-1])
Output:-
(50, 40, 30, 20, 10)
English
A negative step can be used to access tuple elements in reverse order.
Important Point :-
Tuple slicing करने पर नई Tuple प्राप्त होती है। Original Tuple में कोई बदलाव नहीं होता।
numbers = (10, 20, 30, 40, 50)
result = numbers[1:4]
print(result)
print(numbers)
Output:-
(20, 30, 40)
(10, 20, 30, 40, 50)
English
Slicing does not modify the original tuple. It returns a new tuple containing the selected elements.
In Short :-
Slicing → Tuple के एक या अधिक elements को range के रूप में access करना।
Start → Included
Stop → Excluded
Step → Elements के बीच का gap निर्धारित करता है।
Negative Step → Reverse order में elements access करने के लिए।