Python Tuple क्या है? Tuple Creation, Indexing, Slicing & Methods | Python Tuple in Hindi | RBSE Class 12

इस लेख में: Python Tuple क्या है, Tuple कैसे create करते हैं, indexing, negative indexing, slicing, packing, unpacking और important methods count() और index() को आसान Hindi में examples के साथ समझेंगे।

Python Tuple क्या है?

Tuple Python का एक built-in collection data type है, जिसका उपयोग multiple values को एक sequence में store करने के लिए किया जाता है।

Tuple की एक महत्वपूर्ण विशेषता यह है कि यह immutable होता है। इसका अर्थ है कि Tuple create करने के बाद उसके existing elements को सामान्य तरीके से बदला नहीं जा सकता।

Example

numbers = (10, 20, 30, 40)

print(numbers)

Output:

(10, 20, 30, 40)

Python Tuple की मुख्य विशेषताएँ

  • Tuple ordered collection है।
  • Tuple immutable होता है।
  • Tuple duplicate values को allow करता है।
  • Tuple indexing को support करता है।
  • Tuple slicing को support करता है।
  • Tuple में अलग-अलग data types की values रखी जा सकती हैं।
  • Tuple को parentheses ( ) के साथ लिखा जा सकता है।

Python में Tuple कैसे बनाते हैं?

fruits = ("Apple", "Banana", "Mango")

print(fruits)

Output:

('Apple', 'Banana', 'Mango')

Empty Tuple

बिना elements वाले Tuple को empty tuple कहा जाता है।

my_tuple = ()

print(my_tuple)

Output:

()

Single Element Tuple

एक element वाला Tuple बनाते समय comma लगाना महत्वपूर्ण है।

numbers = (10,)

print(type(numbers))

Output:

<class 'tuple'>
Exam Tip: (10) केवल parentheses में लिखा हुआ number है, जबकि (10,) एक Tuple है। Single-element Tuple में comma महत्वपूर्ण है।

Tuple बिना Parentheses के

Python में tuple packing के कारण comma-separated values से भी Tuple बनाया जा सकता है।

numbers = 10, 20, 30

print(numbers)
print(type(numbers))

Output:

(10, 20, 30)
<class 'tuple'>

Tuple में Different Data Types

data = (10, "Python", 5.5, True)

print(data)

Output:

(10, 'Python', 5.5, True)

Python Tuple Indexing

Tuple के प्रत्येक element का एक index होता है। Positive indexing 0 से शुरू होती है।

fruits = ("Apple", "Banana", "Mango", "Orange")

print(fruits[0])
print(fruits[1])
print(fruits[2])
print(fruits[3])

Output:

Apple
Banana
Mango
Orange

Negative Indexing

Negative indexing में Tuple के elements को last से access किया जाता है।

fruits = ("Apple", "Banana", "Mango", "Orange")

print(fruits[-1])
print(fruits[-2])
print(fruits[-3])

Output:

Orange
Mango
Banana

Tuple में Element Change क्यों नहीं कर सकते?

Tuple immutable होता है। इसलिए existing element को direct assignment से change करने पर error मिलेगा।

numbers = (10, 20, 30)

numbers[1] = 50

ऊपर दिए गए code में Tuple के element को बदलने का प्रयास किया गया है, जो सामान्य tuple assignment के लिए valid नहीं है।

Important: List mutable है, जबकि Tuple immutable है। यह RBSE और Python exams के लिए बहुत महत्वपूर्ण difference है।

Tuple Slicing

Tuple के किसी हिस्से को प्राप्त करने के लिए slicing का उपयोग किया जा सकता है।

Syntax

tuple[start:stop:step]

Example

numbers = (10, 20, 30, 40, 50)

print(numbers[1:4])

Output:

(20, 30, 40)

Tuple Slicing के अन्य Examples

numbers = (10, 20, 30, 40, 50)

print(numbers[:3])
print(numbers[2:])
print(numbers[:])
print(numbers[::2])

Output:

(10, 20, 30)
(30, 40, 50)
(10, 20, 30, 40, 50)
(10, 30, 50)

Tuple को Reverse करना

Slicing की सहायता से Tuple को reverse order में प्राप्त किया जा सकता है।

numbers = (1, 2, 3, 4, 5)

print(numbers[::-1])

Output:

(5, 4, 3, 2, 1)

Tuple की Length

Tuple में कितने elements हैं यह जानने के लिए len() function का उपयोग किया जा सकता है।

fruits = ("Apple", "Banana", "Mango")

print(len(fruits))

Output:

3

Tuple में Element Check करना

किसी value के Tuple में मौजूद होने की जाँच के लिए in operator का उपयोग किया जा सकता है।

fruits = ("Apple", "Banana", "Mango")

if "Mango" in fruits:
    print("Mango is present")

Output:

Mango is present

Tuple पर for Loop

fruits = ("Apple", "Banana", "Mango")

for fruit in fruits:
    print(fruit)

Output:

Apple
Banana
Mango

Tuple count() Method

count() method किसी element की occurrences की संख्या बताता है।

numbers = (10, 20, 10, 30, 10)

print(numbers.count(10))

Output:

3

Tuple index() Method

index() method किसी value की पहली matching occurrence का index बताता है।

fruits = ("Apple", "Banana", "Mango")

print(fruits.index("Banana"))

Output:

1

Tuple Packing

जब multiple values को एक single tuple में pack किया जाता है तो इसे Tuple Packing कहा जाता है।

student = "Amit", 18, "Python"

print(student)

Output:

('Amit', 18, 'Python')

Tuple Unpacking

Tuple के elements को अलग-अलग variables में assign करने को Tuple Unpacking कहा जाता है।

student = ("Amit", 18, "Python")

name, age, course = student

print(name)
print(age)
print(course)

Output:

Amit
18
Python
Exam Tip: Packing में multiple values एक Tuple में आती हैं, जबकि unpacking में Tuple की values अलग-अलग variables में assign की जाती हैं।

Tuple को List में Convert करना

Tuple को list में convert करने के लिए list() function का उपयोग किया जा सकता है।

numbers = (10, 20, 30)

my_list = list(numbers)

print(my_list)

Output:

[10, 20, 30]

List को Tuple में Convert करना

numbers = [10, 20, 30]

my_tuple = tuple(numbers)

print(my_tuple)

Output:

(10, 20, 30)

Nested Tuple

एक Tuple के अंदर दूसरा Tuple store किया जा सकता है। इसे Nested Tuple कहा जाता है।

students = (
    ("Amit", 18),
    ("Ravi", 19),
    ("Neha", 18)
)

print(students)

Nested Tuple से Data Access करना

students = (
    ("Amit", 18),
    ("Ravi", 19)
)

print(students[0])
print(students[0][0])
print(students[1][1])

Output:

('Amit', 18)
Amit
19

Tuple Concatenation

दो Tuples को + operator से combine किया जा सकता है।

a = (1, 2, 3)
b = (4, 5, 6)

c = a + b

print(c)

Output:

(1, 2, 3, 4, 5, 6)

Tuple Repetition

* operator की सहायता से Tuple को repeat किया जा सकता है।

numbers = (1, 2)

print(numbers * 3)

Output:

(1, 2, 1, 2, 1, 2)

Tuple में Maximum और Minimum

Numbers वाले Tuple पर max() और min() functions का उपयोग किया जा सकता है।

numbers = (10, 50, 20, 80, 30)

print("Maximum =", max(numbers))
print("Minimum =", min(numbers))

Output:

Maximum = 80
Minimum = 10

Tuple का Sum

numbers = (10, 20, 30, 40)

print("Sum =", sum(numbers))

Output:

Sum = 100

Tuple में कितने Methods होते हैं?

Tuple के प्रमुख built-in methods की संख्या List की तुलना में कम होती है, क्योंकि Tuple immutable होता है।

Method उपयोग
count() किसी value की occurrences count करना
index() Value का पहला index पता करना
Exam Point: Tuple के commonly used methods count() और index() हैं।

List और Tuple में अंतर

List Tuple
List mutable होती है। Tuple immutable होता है।
Square brackets [ ] का उपयोग किया जाता है। Parentheses ( ) का उपयोग सामान्यतः किया जाता है।
List में कई modification methods उपलब्ध हैं। Tuple में modification methods उपलब्ध नहीं हैं।
Example: [10, 20, 30] Example: (10, 20, 30)
append(), remove(), pop() आदि methods उपलब्ध हैं। मुख्य methods count() और index() हैं।

Tuple और String में समानता

Tuple और String दोनों sequence types हैं। दोनों में indexing और slicing का उपयोग किया जा सकता है और दोनों immutable होते हैं।

Python Tuple के फायदे

  • Immutable होने के कारण data को accidental modification से बचाने में मदद कर सकता है।
  • Multiple values को एक sequence में store किया जा सकता है।
  • Indexing और slicing उपलब्ध हैं।
  • Tuple unpacking उपयोगी है।
  • Data की fixed collection represent करने के लिए उपयोगी है।

Python Tuple का Practical Example

student = ("Rahul", 18, "Class 12", 85.5)

name, age, class_name, percentage = student

print("Name:", name)
print("Age:", age)
print("Class:", class_name)
print("Percentage:", percentage)

Output:

Name: Rahul
Age: 18
Class: Class 12
Percentage: 85.5

Common Mistakes in Python Tuple

1. Single Element Tuple में Comma भूलना

a = (10)

print(type(a))

यह Tuple नहीं बल्कि integer है।

सही तरीका:

a = (10,)

print(type(a))

2. Tuple को Directly Modify करना

Tuple immutable होने के कारण उसके existing elements को direct assignment से बदलना valid नहीं है।

3. Unpacking में Variables की संख्या गलत रखना

data = (10, 20, 30)

a, b = data

यहाँ तीन values को दो variables में unpack करने का प्रयास किया गया है, इसलिए यह valid unpacking नहीं है।

Python Tuple Quick Revision

Concept मुख्य बात
Tuple Ordered और immutable collection
Indexing Elements को index से access करना
Slicing Tuple का एक हिस्सा प्राप्त करना
count() Occurrences count करना
index() Value का पहला index पता करना
Packing Multiple values को Tuple में रखना
Unpacking Tuple values को अलग variables में assign करना
Immutable Existing elements को direct modify नहीं किया जा सकता

RBSE Class 12 Important Questions

  1. Python Tuple क्या है?
  2. Tuple की मुख्य विशेषताएँ लिखिए।
  3. Tuple और List में अंतर लिखिए।
  4. Tuple immutable क्यों कहलाता है?
  5. Python में Tuple कैसे create करते हैं?
  6. Single element Tuple कैसे बनाते हैं?
  7. Tuple indexing क्या है?
  8. Negative indexing क्या है?
  9. Tuple slicing क्या है?
  10. Tuple Packing क्या है?
  11. Tuple Unpacking क्या है?
  12. Tuple के दो प्रमुख methods लिखिए।
  13. count() method का उपयोग क्या है?
  14. index() method का उपयोग क्या है?
  15. List को Tuple में कैसे convert करते हैं?
  16. Tuple को List में कैसे convert करते हैं?
  17. Nested Tuple क्या है?

Frequently Asked Questions (FAQs)

Python Tuple क्या है?

Tuple Python का ordered और immutable collection data type है जिसका उपयोग multiple values को store करने के लिए किया जाता है।

क्या Tuple को बदला जा सकता है?

Tuple immutable होता है, इसलिए उसके existing elements को सामान्य assignment से directly बदला नहीं जा सकता।

Single element Tuple कैसे बनाते हैं?

Single element Tuple बनाने के लिए value के बाद comma लगाना आवश्यक है, जैसे (10,)

Tuple के प्रमुख methods कौन-से हैं?

Tuple के प्रमुख methods count() और index() हैं।

Tuple और List में सबसे महत्वपूर्ण अंतर क्या है?

List mutable होती है जबकि Tuple immutable होता है।

अब अगला Topic पढ़ें

RBSE Python Study Tip: Tuple में सबसे पहले immutable concept, indexing, slicing, packing, unpacking तथा count() और index() methods को अच्छी तरह समझें। इसके बाद List और Tuple के differences को exam के लिए revise करें।
Official Reference:

Python Tuple और sequence operations की विस्तृत जानकारी के लिए Python की official documentation देखें।

Python Data Structures Documentation