Python में tuple भी list की तरह ही 4 built in data types (list , tuple , set , dictionary) में से एक है जिसका use data collections को store करने किया जाता है। data collections means एक values का collection या कह सकते हैं multiple values store करता है।


Python में tuple ordered and unchangeable है। और duplicate को भी allow करता है।

  1. Ordered - ordered मतलब जिस order में हमने items को list में insert किया था हमें उसी order में मिलेंगे।

  2. Duplicate - duplicate means हम same item को कई बार insert कर सकते हैं।

  3. Unchangeable - मतलब उन items को जरूरत पड़ने पर tuple items add , remove या delete नहीं कर सकते हैं।


Important
Python list और tuple में यही सबसे बड़ा difference है , कि list के items को directly हम कभी update , remove या insert कर सकते हैं जबकि tuple में नहीं।

tuple को हम parenthesis () use करके create कर सकते हैं। tuple item का indexed collection होता है मतलब हर item का index number होता है जो कि 0 से start होता है। n numbers के items की tuple के लिए maximum index number n - 1 होगी।

Python tuple Example

Copy Fullscreen Close Fullscreen Run
t = (12,45,67,8990,78)
print(t)

#accessing single item.
print('It is first item :', t[0])
print('It is third item :', t[2])
print('It is last item :', t[4])
Output
C:\Users\Rahulkumar\Desktop\python>python tuple_ex.py
[12, 45, 67, 8990, 78]
It is first item : 12
It is third item : 67
It is last item : 78

Note : यह जरूरी नहीं है कि tuple में सिर्फ same type ही items हों , आप अपनी need के according किसी भी type (String , Boolean , Numeric) के elements insert कर सकते हैं।
For Example - tuple_var = ("name", 34, 56.78, True)

Python tuple type() and len()

tuple की length जानने के लिए len() function का use किया जाता है और , type जानने के लिए type() function का use किया जाता है।

Copy Fullscreen Close Fullscreen Run
t = ("name", 34, 56.78, True)
print('Type :', type(t))
print('Length :', len(t))
Output
C:\Users\Rahulkumar\Desktop\python>python tuple_type.py
Type : <class 'tuple'>
Length : 4
Remember

tuple में जब कोई single element होता है तो उसका type tuple न होकर उस single item का ही type होता है। मतलब अगर parenthesis के अंदर सिर्फ एक ही element है , तो उसका type tuple नहीं होता है।
See Example -

Copy Fullscreen Close Fullscreen Run
t1 = (12)
print(type(t1))

t2 = ('Rahul')
print(type(t2))

t3 = (True)
print(type(t3))

t4 = (5.6)
print(type(t4))
Output
C:\Users\Rahulkumar\Desktop\python>python tuple_type.py
<class 'int'>
<class 'str'>
<class 'bool'>
<class 'float'>

इसलिए tuple पर कोई operation perform करने से पहले ये ध्यान रखें।

Python Iterating tuple

चूंकि Indexed collection होने की वजह से ही इन्हे भी list की तरह ही for loop / while loop की help से iterate कर सकते हैं।

Copy Fullscreen Close Fullscreen Run
t = ("name", 34, 56.78, True)

print('Iterate using for loop :')
for item in t :
    print(item)

#for line break.
print('')
print('Now iterate using while loop :')
no=0
while no < len(t) :
    print(t[no])
    no += 1
Output
C:\Users\Rahulkumar\Desktop\python>python tuple_iterate.py
Iterate using for loop :
name
34
56.78
True

Now iterate using while loop :
name
34
56.78
True

Python Accessing tuple Items

चूंकि items index number से associate होते हैं इसलिए हम direct index number से तो access कर ही सकते हैं इसके आलावा colon : का use करके हम एक particular tuple का part भी access कर सकते हैं।

Copy Fullscreen Close Fullscreen Run
tuple_var = [12,45,56,5,76,78,78,45,12]
print(tuple_var[4])  #get specified item.
print(tuple_var[2:])  #all items from 3rd index.
print(tuple_var[4:6])  #all items from 4th to 6th index (6th index item को छोड़कर) .
print(tuple_var[:5])  #all items from starting to 5th index. 
print(tuple_var[-1])  #last item. 
print(tuple_var[3:-1])  #all items from 3rd index to n-1 th index (-1th index item को छोड़कर) .
print(tuple_var[-8:-1])  #all items from 8th index from last to -1 index from last (-1th index item को छोड़कर).
Output
C:\Users\Rahulkumar\Desktop\python>python tuple_access.py
76
[56, 5, 76, 78, 78, 45, 12]
[76, 78]
[12, 45, 56, 5, 76]
12
[5, 76, 78, 78, 45]
[45, 56, 5, 76, 78, 78, 45]

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers