Python में कई बार हमें ऐसे Data को Store करना होता है जिसे Program के दौरान बदला (Modify) नहीं जाना चाहिए। उदाहरण के लिए किसी विद्यार्थी का रोल नंबर, जन्म तिथि, देश का ISO Code, सप्ताह के दिनों के नाम या निश्चित (Fixed) Configuration Values।
ऐसी परिस्थितियों में Tuple सबसे उपयुक्त Data Type है।
Tuple, List की तरह एक Ordered Collection है, लेकिन List के विपरीत Tuple Immutable होता है। अर्थात Tuple बनने के बाद उसके Elements को बदला, जोड़ा या हटाया नहीं जा सकता।
Tuple तेज़ (Fast), सुरक्षित (Safe) और Memory Efficient होता है। इसी कारण Python के कई Built-in Functions और Libraries Tuple का उपयोग करती हैं।
इस अध्याय में हम Tuple की मूल अवधारणा से लेकर Tuple Creation, Packing, Unpacking, Indexing, Slicing, Nested Tuple, Built-in Functions, Tuple Methods और Practical Programs तक सभी विषयों को विस्तार से समझेंगे।
Tuple Python का एक Built-in Collection Data Type है, जिसमें कई Values को Parentheses ( ) के अंदर Comma (,) द्वारा अलग करके लिखा जाता है।
Tuple में किसी भी प्रकार का Data Store किया जा सकता है।
tuple_name = (item1, item2, item3)
student = ("Rahul", 20, "BCA")
Tuple की विशेषताएँ (Characteristics)
- Ordered Collection है।
- Immutable होता है।
- Duplicate Values की अनुमति होती है।
- Different Data Types Store किए जा सकते हैं।
- Positive एवं Negative Indexing उपलब्ध होती है।
- Nested Tuple बनाया जा सकता है।
- List की तुलना में Memory कम उपयोग करता है।
- List की तुलना में Iteration तेज़ होती है।
colors = ("Red", "Green", "Blue")
Python में Parentheses आवश्यक नहीं हैं।
colors = "Red", "Green", "Blue"
data = ()
numbers = tuple([10, 20, 30])
letters = tuple("Python")
print(letters)
Output
('P', 'y', 't', 'h', 'o', 'n')
यदि Tuple में केवल एक Element हो, तो उसके बाद Comma लगाना आवश्यक है।
❌ गलत
data = (10)
यह Integer माना जाएगा।
✔ सही
data = (10,)
data = (
"Rahul",
22,
78.5,
True,
("Math", "Science")
)
numbers = (10,20,30,40)
print(len(numbers))
Output
4
Element : Apple Mango Banana Orange
Index : 0 1 2 3
Negative Index
Element : Apple Mango Banana Orange
Index : -4 -3 -2 -1
fruits = ("Apple","Mango","Banana")
print(fruits[1])
Output
Mango
print(fruits[-1])
Output
Banana
numbers = (10,20,30,40,50)
print(numbers[1:4])
Output
(20, 30, 40)
print(numbers[::-1])
Output
(50,40,30,20,10)
Tuple बनने के बाद उसके Elements को बदला नहीं जा सकता।
❌ गलत
numbers = (10,20,30)
numbers[1] = 50
Output
TypeError
fruits = ("Apple","Mango","Banana")
for item in fruits:
print(item)
print("Apple" in fruits)
print("Orange" not in fruits)
कई Values को एक Tuple में Store करना Packing कहलाता है।
student = ("Rahul",20,"BCA")
या
student = "Rahul",20,"BCA"
Tuple के प्रत्येक Element को अलग-अलग Variables में Store करना Unpacking कहलाता है।
student = ("Rahul",20,"BCA")
name, age, course = student
print(name)
print(age)
print(course)
numbers = (10,20,30,40,50)
a,*b,c = numbers
print(a)
print(b)
print(c)
Output
10
[20,30,40]
50
a=(1,2)
b=(3,4)
print(a+b)
Output
(1,2,3,4)
print((10,20)*3)
Output
(10,20,10,20,10,20)
students=(
("Rahul",85),
("Amit",90),
("Neha",88)
)
Access
print(students[0][0])
Output
Rahul
|
Function |
कार्य |
|
len() |
कुल Elements |
|
max() |
सबसे बड़ा |
|
min() |
सबसे छोटा |
|
sum() |
योग |
|
sorted() |
Sorting |
|
any() |
कोई True |
|
all() |
सभी True |
Tuple में केवल दो Methods होते हैं।
|
Method |
उपयोग |
|
count() |
किसी Element की संख्या |
|
index() |
Element का पहला Index |
numbers=(10,20,10,30,10)
print(numbers.count(10))
Output
3
numbers=(10,20,30)
print(numbers.index(20))
Output
1
|
List |
Tuple |
|
Mutable |
Immutable |
|
Square Brackets [] |
Parentheses () |
|
Methods अधिक |
केवल 2 Methods |
|
Memory अधिक |
Memory कम |
|
Speed कम |
Speed अधिक |
|
Data बदल सकता है |
Data नहीं बदल सकता |
subjects=("Math","Science","English")
for sub in subjects:
print(sub)
numbers=(12,45,89,23)
print(max(numbers))
marks=(70,80,90)
print(sum(marks))
cities=("Delhi","Lucknow","Kanpur")
if "Lucknow" in cities:
print("Found")
student=("Rahul",21,"BCA")
name,age,course=student
print(name)
print(age)
print(course)
data=(10,20)
data[0]=50
❌ Tuple Immutable है।
data=(10,20)
print(data.index(50))
❌ क्योंकि Value मौजूद नहीं है।
- Fixed Data के लिए Tuple का उपयोग करें।
- यदि Data बदलना हो तो List का उपयोग करें।
- Function से Multiple Values Return करने के लिए Tuple उपयुक्त है।
- Unpacking का उपयोग Code को सरल बनाता है।
- Immutable Data के लिए Tuple अधिक सुरक्षित है।
परीक्षा की दृष्टि से महत्वपूर्ण तथ्य
- Tuple एक Immutable Ordered Collection है।
- Tuple Parentheses ( ) में लिखा जाता है।
- Single Element Tuple में Comma आवश्यक है।
- Tuple में केवल count() और index() Methods होते हैं।
- Packing और Unpacking Tuple की महत्वपूर्ण विशेषताएँ हैं।
- Tuple List की तुलना में अधिक Fast और Memory Efficient होता है।
Frequently Asked Questions (FAQs)
Tuple Python का Immutable Collection Data Type है।
प्रश्न 2. Tuple Mutable है या Immutable?
Immutable।
प्रश्न 3. Tuple में कितने Methods होते हैं?
केवल दो—count() और index()।
प्रश्न 4. Packing और Unpacking क्या है?
Values को Tuple में Store करना Packing और Tuple से अलग-अलग Variables में Values निकालना Unpacking कहलाता है।
प्रश्न 5. List और Tuple में मुख्य अंतर क्या है?
List Mutable होती है जबकि Tuple Immutable होता है।
- Tuple = Ordered Collection
- Immutable = हाँ
- Duplicate = Allowed
- Mixed Data Types = Allowed
- Indexing = Supported
- Slicing = Supported
- Packing = Multiple Values एक Tuple में
- Unpacking = Tuple से Variables में Values
- Methods = count(), index()
Python Dictionary in Hindi – Dictionary, Keys, Values, Dictionary Methods, Nested Dictionary, Built-in Functions और Practical Programs। इसमें Dictionary के सभी Concepts को विस्तार से सीखेंगे।