Python me Tuple kya hota hai?


Tuple Python ka ek ordered collection hai — bilkul list ki tarah — lekin iska sabse bada fark yeh hai ki Tuple immutable hota hai. Matlab ek baar banane ke baad isme koi bhi change nahi ho sakta — na add, na remove, na update.

Tuple ki 4 main khasiyatein hain :-

Is page me yeh sab sikhenge :-



1. Tuple Banana aur Access Karna

Tuple ko round brackets ( ) me likha jaata hai aur items ko comma ( , ) se alag kiya jaata hai.

Example 1: Tuple banana — different types ke items

Python
In [1]:
1
Output

Example 2: Tuple access karna — indexing

Python
In [1]:
1
Output


2. Indexing aur Slicing

Tuple ki slicing bilkul list aur string jaisi hoti hai — [start : stop : step] syntax use hota hai.

Example 1: Slicing — tuple ka ek hissa nikalna

Python
In [1]:
1
Output

Example 2: in operator — check karna ki value tuple me hai ya nahi

Python
In [1]:
1
Output


3. Tuple Immutability

Tuple ka sabse important feature — yeh change nahi ho sakta. Ek baar banane ke baad add, remove ya update karne ki koshish karo toh TypeError aayega. Lekin agar change zaroori ho toh tuple ko list me convert kar sakte hain.

Example 1: Change karne ki koshish — error aayega

Python
In [1]:
1
Output

Example 2: Workaround — tuple ko list me convert karke change karo

Python
In [1]:
1
Output


4. Tuple Methods — count() aur index()

Tuple ke paas sirf 2 built-in methods hote hain — kyunki immutable hone ki wajah se add/remove waale methods nahi hote.

Example 1: count() — value kitni baar aayi

Python
In [1]:
1
Output

Example 2: index() — value kahan hai

Python
In [1]:
1
Output


5. Tuple Packing aur Unpacking

Packing — multiple values ko ek tuple me band karna.
Unpacking — tuple ki values ko alag alag variables me dalna. Yeh Python ka ek bahut useful feature hai.

Example 1: Basic packing aur unpacking

Python
In [1]:
1
Output

Example 2: * (star) se unpacking — baaki sab ek list me

Python
In [1]:
1
Output

Example 3: Tuple unpacking se do variables swap karna

Python
In [1]:
1
Output


6. Tuple vs List — Kab kaun use karein?

Dono ordered collections hain — lekin dono ka use alag alag situation me hota hai.

FeatureList [ ]Tuple ( )
Mutable?✅ Haan❌ Nahi
SpeedThodi slowFaster
MemoryZyadaKam
MethodsBahut saareSirf 2 (count, index)
Use kab kareinJab data change hone wala hoJab data fixed rehna chahiye
ExampleShopping cart, to-do listCoordinates, RGB colors, DB records

Example 1: Real use case — coordinates aur RGB colors

Python
In [1]:
1
Output


7. Tuple ke saath Loops aur Operations

Tuple pe for loop, concatenation, repetition aur conversion (list ↔ tuple) — sab kuch kiya ja sakta hai.

Example 1: For loop se tuple iterate karna

Python
In [1]:
1
Output

Example 2: Concatenation, repetition aur conversion

Python
In [1]:
1
Output



Quick Summary — Tuple ek nazar me

ConceptKaamExample
Banana( ) me itemst = (1, 2, 3)
Single itemComma zaroorit = (5,)
IndexingItem access karot[0], t[-1]
SlicingHissa nikalot[1:4]
count(x)Kitni baar aayat.count(5)
index(x)Kahan hait.index(5)
UnpackingVariables me daaloa, b, c = t
* unpackingBaaki sab ek list mea, *rest = t
SwapVariables badloa, b = b, a
tuple()List → Tupletuple([1,2,3])
list()Tuple → Listlist((1,2,3))