मुख्य कंटेंट पर जाएं
Python Tuple in Hindi – Tuple, Packing, Unpacking, Tuple Methods और Practical Programs
PYTHON PROGRAMMING 12 मिनट पढ़ें 1 बार पढ़ा गया

Python Tuple in Hindi – Tuple, Packing, Unpacking, Tuple Methods और Practical Programs

Lavkush Chakrawarti

परिचय

Tuple क्या है?

Tuple का Syntax

उदाहरण

Tuple की विशेषताएँ (Characteristics)

Tuple बनाने के तरीके

1. Parentheses का उपयोग

2. Parentheses के बिना

3. Empty Tuple

4. tuple() Constructor

5. String से Tuple

Single Element Tuple

Tuple में विभिन्न Data Types

Tuple Length

Tuple Indexing

Positive Indexing

Negative Indexing

Tuple Slicing

Tuple Immutable क्यों है?

Tuple Traversing

Membership Operators

Tuple Packing

Tuple Unpacking

Extended Unpacking

Tuple Concatenation

Tuple Repetition

Nested Tuple

Built-in Functions

Tuple Methods

count()

index()

List और Tuple में अंतर

Practical Programs

Program 1 – Tuple Print

Program 2 – Maximum Number

Program 3 – Total Marks

Program 4 – Tuple Search

Program 5 – Tuple Unpacking

Common Errors

TypeError

ValueError

Best Practices

परीक्षा की दृष्टि से महत्वपूर्ण तथ्य

Frequently Asked Questions (FAQs)

प्रश्न 1. Tuple क्या है?

प्रश्न 2. Tuple Mutable है या Immutable?

प्रश्न 3. Tuple में कितने Methods होते हैं?

प्रश्न 4. Packing और Unpacking क्या है?

प्रश्न 5. List और Tuple में मुख्य अंतर क्या है?

Quick Revision

अगला अध्याय

 

परिचय

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 क्या है?

Tuple Python का एक Built-in Collection Data Type है, जिसमें कई Values को Parentheses ( ) के अंदर Comma (,) द्वारा अलग करके लिखा जाता है।

Tuple में किसी भी प्रकार का Data Store किया जा सकता है।

 

Tuple का Syntax

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 तेज़ होती है।

 

Tuple बनाने के तरीके

  1. Parentheses का उपयोग

colors = ("Red", "Green", "Blue")

 

  1. Parentheses के बिना

Python में Parentheses आवश्यक नहीं हैं।

colors = "Red", "Green", "Blue"

 

  1. Empty Tuple

data = ()

 

  1. tuple() Constructor

numbers = tuple([10, 20, 30])

 

  1. String से Tuple

letters = tuple("Python")

 

print(letters)

Output

('P', 'y', 't', 'h', 'o', 'n')

 

Single Element Tuple

यदि Tuple में केवल एक Element हो, तो उसके बाद Comma लगाना आवश्यक है।

❌ गलत

data = (10)

यह Integer माना जाएगा।

✔ सही

data = (10,)

 

Tuple में विभिन्न Data Types

data = (

    "Rahul",

    22,

    78.5,

    True,

    ("Math", "Science")

)

 

Tuple Length

numbers = (10,20,30,40)

 

print(len(numbers))

Output

4

 

Tuple Indexing

Element : Apple   Mango   Banana   Orange

Index   :   0       1        2        3

Negative Index

Element : Apple   Mango   Banana   Orange

Index   :  -4      -3       -2       -1

 

Positive Indexing

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

 

print(fruits[1])

Output

Mango

 

Negative Indexing

print(fruits[-1])

Output

Banana

 

Tuple Slicing

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

 

print(numbers[1:4])

Output

(20, 30, 40)

 

print(numbers[::-1])

Output

(50,40,30,20,10)

 

Tuple Immutable क्यों है?

Tuple बनने के बाद उसके Elements को बदला नहीं जा सकता।

❌ गलत

numbers = (10,20,30)

 

numbers[1] = 50

Output

TypeError

 

Tuple Traversing

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

 

for item in fruits:

    print(item)

 

Membership Operators

print("Apple" in fruits)

print("Orange" not in fruits)

 

Tuple Packing

कई Values को एक Tuple में Store करना Packing कहलाता है।

student = ("Rahul",20,"BCA")

या

student = "Rahul",20,"BCA"

 

Tuple Unpacking

Tuple के प्रत्येक Element को अलग-अलग Variables में Store करना Unpacking कहलाता है।

student = ("Rahul",20,"BCA")

 

name, age, course = student

 

print(name)

print(age)

print(course)

 

Extended Unpacking

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

 

a,*b,c = numbers

 

print(a)

print(b)

print(c)

Output

10

[20,30,40]

50

 

Tuple Concatenation

a=(1,2)

b=(3,4)

 

print(a+b)

Output

(1,2,3,4)

 

Tuple Repetition

print((10,20)*3)

Output

(10,20,10,20,10,20)

 

Nested Tuple

students=(

    ("Rahul",85),

    ("Amit",90),

    ("Neha",88)

)

Access

print(students[0][0])

Output

Rahul

 

Built-in Functions

Function

कार्य

len()

कुल Elements

max()

सबसे बड़ा

min()

सबसे छोटा

sum()

योग

sorted()

Sorting

any()

कोई True

all()

सभी True

 

Tuple Methods

Tuple में केवल दो Methods होते हैं।

Method

उपयोग

count()

किसी Element की संख्या

index()

Element का पहला Index

 

count()

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

 

print(numbers.count(10))

Output

3

 

index()

numbers=(10,20,30)

 

print(numbers.index(20))

Output

1

 

List और Tuple में अंतर

List

Tuple

Mutable

Immutable

Square Brackets []

Parentheses ()

Methods अधिक

केवल 2 Methods

Memory अधिक

Memory कम

Speed कम

Speed अधिक

Data बदल सकता है

Data नहीं बदल सकता

 

Practical Programs

Program 1 – Tuple Print

subjects=("Math","Science","English")

 

for sub in subjects:

    print(sub)

 

Program 2 – Maximum Number

numbers=(12,45,89,23)

 

print(max(numbers))

 

Program 3 – Total Marks

marks=(70,80,90)

 

print(sum(marks))

 

Program 4 – Tuple Search

cities=("Delhi","Lucknow","Kanpur")

 

if "Lucknow" in cities:

    print("Found")

 

Program 5 – Tuple Unpacking

student=("Rahul",21,"BCA")

 

name,age,course=student

 

print(name)

print(age)

print(course)

 

Common Errors

TypeError

data=(10,20)

 

data[0]=50

❌ Tuple Immutable है।

 

ValueError

data=(10,20)

 

print(data.index(50))

❌ क्योंकि Value मौजूद नहीं है।

 

Best Practices

  • 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)

प्रश्न 1. Tuple क्या है?

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 होता है।

 

Quick Revision

  • 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 को विस्तार से सीखेंगे।

 

निष्कर्ष (Conclusion)

Python Tuple की पूरी जानकारी हिंदी में। Tuple क्या है, Tuple Creation, Indexing, Slicing, Packing, Unpacking, Tuple Methods, Built-in Functions, Nested Tuple, Practical Programs और Interview Questions सीखें।