tuples in Python

Python में data को store करने के लिए List की तरह एक और data structure होता है, जिसे Tuple कहा जाता है।

Tuple भी multiple values को store करने के लिए उपयोग किया जाता है, लेकिन List से इसका सबसे बड़ा अंतर यह है कि Tuple immutable होता है।

इसका मतलब है कि एक बार Tuple बन जाने के बाद उसके elements को change नहीं किया जा सकता।


Tuple क्या होता है

Tuple items का एक ordered collection होता है, जिसे round brackets ( ) के अंदर लिखा जाता है।

इसके elements को comma , से अलग किया जाता है।


Example

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

Output

(10, 20, 30)

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

  • Ordered होता है (elements का order fix रहता है)
  • Immutable होता है (change नहीं किया जा सकता)
  • Duplicate values allow करता है
  • Different data types store कर सकता है

Tuple Indexing

Tuple के elements को index की मदद से access किया जाता है।

data = (10, 20, 30, 40)
print(data[0])
print(data[2])

Output

10
30

Negative Indexing

data = (10, 20, 30)
print(data[-1])

Output

30

Tuple Slicing

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

data = (10, 20, 30, 40, 50)
print(data[1:4])

Output

(20, 30, 40)

Tuple को modify नहीं किया जा सकता

data = (10, 20, 30)
data[1] = 25

यह error देगा क्योंकि tuple immutable होता है।


Tuple Methods

Tuple में बहुत कम methods होते हैं:

1. count()

data = (1, 2, 2, 3)
print(data.count(2))

Output

2

2. index()

data = (10, 20, 30)
print(data.index(20))

Output

1

Tuple Packing और Unpacking

Packing

data = 10, 20, 30
print(data)

Unpacking

a, b, c = (10, 20, 30)
print(a)
print(b)
print(c)

Real-life Example: Coordinates

point = (3, 5)
print("X =", point[0])
print("Y =", point[1])

Explanation
यह example coordinate system (x, y) को represent करता है।


Real-life Example: Fixed data (Date)

date = (27, 3, 2026)
print("Day:", date[0])
print("Month:", date[1])
print("Year:", date[2])

Explanation
Date जैसे fixed values को tuple में store करना अच्छा practice है।


List और Tuple में अंतर

ListTuple
MutableImmutable
[ ] use होता है( ) use होता है
Modify कर सकते हैंModify नहीं कर सकते

Common mistakes

  1. tuple को list की तरह modify करने की कोशिश करना
  2. single element tuple में comma भूल जाना
a = (5)    # यह tuple नहीं है
a = (5,) # यह सही tuple है

Simple complete program

data = (1, 2, 3, 4)
for item in data:
print(item)

इस topic की मुख्य बात

Tuple Python का एक important data structure है, जो fixed data को store करने के लिए उपयोग किया जाता है।

इस topic में student यह सीखता है:

  • tuple क्या होता है
  • indexing और slicing
  • tuple methods
  • packing और unpacking
  • list और tuple में अंतर

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top