This Website for Those Students who are searching for a platform to study online in their own Language Hindi, on this website they will learn all courses like IAS, programming, web designing, data science, and competitive exam preparation, etc.


Python - Tuple Data Type

Python - Tuple Data Type

Python - Tuple Data Type





आज हम इस पोस्ट में python data types Tuple के बारें में पढेंगे तो चलिए शुरू करते इससे पहले आप List के बारे मे पढ़ चुके होंगे दोनों मे कुछ ज्यादा अंतर नहीं है बस कुछ Syntax का फरक है और जो main अंतर वो है की Tuple Data Type immutable (unchangeable) होता है.

Tuple भी list की तरह होता है जो की बहुत सारी अलग अलग वैल्यू को hold करता है और immutable (unchangeable) होता है immutable का यहाँ मतलब है वो data Type या data structure जिसे हम एक बार Create करने के बाद change नहीं कर सकते ऐसे data Type को immutable बोलते है,

और लिस्ट की तुलना में Tuple Fast होता है क्योंकि इसके elements Fix होते है और इनमे कोई भी Change हम कर नहीं सकते.

Python में List और Tuple एक जैसे ही होते है लेकिन List के items को change या उसके items delete मतलब बाद में Modify किये जा सकते है और Tuple के items को change या उसके items delete नहीं किये जा सकते है, Tuples सिर्फ read किये जा सकते है |

Python के tuple में जो elements होते है उसे items कहते है |



Tuple के हर item को comma (,) से seperate किया जाता है और पूरे items को parenthesis(()) के अन्दर close किया जाता है, Tuple में mixed data types भी इस्तेमाल किये जा सकते है |



तो चलिए अब कुछ Basic Operation और Methods के बारे में जान लेते जैसे Tuple को कैसे Create ,Delete और इन पर कैसे-कैसे Operation Perform किये जा सकते है।

Creating Tuple



Python मे Tuple को Create करने के लिए parenthesis(()) मे हर item को comma (,) से seperate करते करते हुए Create किया जाता है जैसे निचे Example में बताया गया है।



  
    
    tuple1 = (1, 2, 3, 4, 5) #Integer Tuple
    
    tuple2 = (1.5, 2.4, 3.8, 4.4, 5.7); #Float Tuple
    
    tuple3 = (1, 2, "Hello", 4.5); #Mixed Data Types Tuple
    
    tuple4 = (1, 2, (1, 2), 4.5) #Nested Tuple


  

Tuple Without Parenthesis

Tuples बिना parenthesis के भी हो सकते है, तो चलिए Example की मदत से समझते है |

  
    
    tuple = "H", "e", "l", "l", "o"
    
    print(type(tuple))
    
    #Output : <class 'tuple'>


  

Tuple Indexing



हर Programming Language में indexing की शुरुआत '0' से होती है उसी तरह से Python में Tuple के पहले item का index '0' होता है |



  

    
    strTuple = ("H", "e", "l", "l", "o")



ItemsNumber item1 item2 item3 item4 item5
Index 0 1 2 3 4
Items "H" "e" "l" "l" "o"


Negative Indexing



Python में Tuple के आखिरी index '-1' से शुरू होता है | जिसकी मदत से हम लास्ट item को Access कर सकते है.



  
    
    greeting  = ("H", "e", "l", "l", "o")


  


ItemsNumber item1 item2 item3 item4 item5
Index -5 -4 -3 -2 -1
Items "H" "e" "l" "l" "o"


Accesing Tuple



index से Tuple के items को access किया जा सकता है |



Syntax :

  
    
    tuple(index)


  


Code :

  
    tuple = (1, 2, "H", 2.8) 
    
    print(tuple[0])
    
    print(tuple[1])
    
    print(tuple[2])
    
    print(tuple[3])
 


Output :

  
    1

    2
    
    H
    
    2.8


  


Invalid Indexing



अगर हम invalid index करते है मतलब वो item access करना चाहते है जो Tuple मे नहीं है तो 'indexError' का exception आ जाता है |



Source Code :

  tuple = (1, 2, "H", 2.8)
print(tuple[10])



Output :

   
    
    print(tuple(10))

    IndexError: tuple index out of range


  


Accessing Nested Tuple

अगर हमे Nested Tuple को Access करना है तो Row और Coloum की मदत से Access कर सकते जिसका Example निचे दिया गया है।



  
  
    tuple = (1, 2, "Hello", ("R", "U"))
    
    print(tuple[3][0]) #Output : R
    
    print(tuple[3][1]) #Output : U


  


Tuples Cannot Change But Reassign



tuple को change नहीं कर सकते लेकिन उसे re-assign किया जा सकता है |



  
  tuple = (1, 2, 3, 4, 5)
  
  print(tuple)
  
  #Output : (1, 2, 3, 4, 5)
  
  tuple = (6, 7, 8, 9, 10)
  
  print(tuple)
  
  #Output : (6, 7, 8, 9, 10)


  


Deleting Tuple



Tuple को delete करने के लिए 'del' Operator से पूरा tuple delete किया जा सकता है लेकिन tuple के एक-एक item को delete नहीं किया जा सकता |



  
  
  tuple = (1, 2, 3, 4, 5)
  
  del tuple[0]
  
  #Output :

  #del tuple[0]

  #TypeError: 'tuple' object doesn't support item deletion

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

  del tuple

  print(tuple)

  #Output : <class 'tuple'>

  


Tuple to Sub-Tuple or Tuple Slicing



colon(:) के left में sub-Tuple कहा से start करना है और colon(:) के right में कहा पर end करना है वो दिया जाता है |

Slicing के लिए colon को 'slicing operator' कहा जाता है |

Syntax :

  tuple(start:end)

  
Source Code :
  
    tuple = (1, 2, "H", 2.8)

    print(tuple[0:1]) # (1)

    print(tuple[0:2]) # (1, 2)

    print(tuple[1:3]) # (2, 'H', 2.8)

    print(tuple[3:1]) #()
 
Output :
  
    (1)

    (1, 2)

    (2, 'H', 2.8)

    ()
    

  


अगर colon(:) के left side का index invalid होता है तो blank tuple(()) return होती है और colon(:) के right side का index invalid होता है तो Tuple के left index से आखिरी तक index; return होता है | अगर दोनों ही invalid index होता है तो blank tuple(()) return होता है |

For Example,

  
    tuple = (1, 2, "H", 2.8)

    print(tuple[0:1]) #Output  (1)

    print(tuple[0:2]) #Output (1, 2)

    print(tuple[1:3]) #Output (2, 'H')

    print(tuple[1:15]) #Output (2, 'H', 2.8)

    print(tuple[10:4]) #Output ()

  



Tuple Concatenation

अगर आपको 2 या ज्यादा Tuple का Concatenation करना होतो आप (+) Operator का use कर सकते है निचे Example की मदत से समझाया गया है।



  
    tuple = (1, 2, "H", 2.8) + ("Hello", (2, 8))

    print (tuple)


    #Output : (1, 2, 'H', 2.8, 'Hello', (2, 8))

  


Iterating of Tuple

दोस्तों अगर आपको Tuple के सारे Element print करने होतो हम loop की मदत से कर सकते है जैसा की निचे Example में बताया गया है जरुरी नहीं की आप Iterating की मदत से print ही करे इसका Use कर के आप सभी Operation perfome कर सकते है जैसे Addition , Multiplication ,in and not-in operators आदि।



  

  tuple = (1, 2, "H", 2.8)

  for i in tuple:
     print(i)

 #Output :
 #1
 #2
 #H
 #2.8


  


Operations on Tuple



दोस्तों Tuple पर हम अलग – अलग ऑपरेटर्स का प्रयोग कर सकते है |

जैसे addition, multiplication, in and not-in operators.

तो चलिए अब एक - एक करके देखते है ।



Addition Operator

तो अब देखते है की कैसे हम 2 या 2 से ज्यादा Tuples को Addition कर सकते है।



        
        
       a=(1,2,3)

       b=(4,5,6)

       a+b
       
      #output (1, 2, 3, 4, 5, 6)
      
  


Multiplication Operator

तो अब देखते है की कैसे हम Tuples का Multiplication कर सकते है।



        
        
       a=(1,2,3)

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


In Operator

दोस्तों In Operator का Use हम जब करते है जब हमे पता करना हो की कोई Item Tuple में है या नहीं अगर वो item Tuple मे True return करता है तो हम वह In Operator कर सकते है।



             
 
       3 in a
      #output  True
       4 in a
      #output   False
      
          
 


Not-In Operator

जैसे हमने जाना In Operator क्या करता है वैसे ही Not-In Operator भी True और False return करता है बस फर्क इतना है की ये जब true retrun करता है जब कोई item Tuple में नहीं होता नहीं तो false retrun करता है,

तो इस Operator Use हम जब करते है जब हमे cheak करना हो की वो item Tuple मे है या नहीं .



                   
     
       3 not in b
      #output True
      
       4 not in b
      #output  False

    
 


Tuple Related built-in Functions

Tuple पर काम करने वाले कुछ Function्स को अब हम उदाहरणों के साथ देखेंगे | यह Function्स है – len(), sum(), min(), max() |

इन Function्स में से sum(), min(), max() Function Tuple पर तभी काम करेंगे जब Tuple के सारे Element्स इन्टिजर Category (int, float) के हो |



len()



हम len() Function की मदद से हम Tuple में कितने item है ये जान सकते है.



        

            
       text=("I", "Love","My","India")
       type(text)
      <class 'tuple' >
       len(text)
      4





जैसाकि हम इस Example में देख सकते है | यहां एक text Tuple है, इसमें चार item है | function len () की मदत से हम tuple की Length पता कर सकते है |



sum()



sum() Function की मदत से हम Tuple के item का sum कर सकते है जैसा की निचे Example की मदत से बताया गया है .



        

          data=(1,20,3.14,30)
          
          print(sum(data))
          
          #output  54.14
          
        



दोस्तों मे आपको बता दू की ऐसा करने के लिए, Tuple के सारे element Category (int, float) के होने चाहिए |

नही तो Error आएगा जैसा की आप निचे देख सकते है |



          

            data=(1, 3.14,"Hello")
       
           
            print(sum(data))

          
        


Output
              

           Traceback (most recent call last):
             File "<pyshell#8>", line 1, in<module>
               sum(data2)
           TypeError: unsupported operand type(s) for +: 'float' and 'str'
  


min()



इस min Function से हम यह जान सकते है की Tuple मे के सबसे छोटा item कौन सा है |



  

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


    print(min(data))

    #output 10




जब Tuple के Element अलग – अलग प्रकार के हो तो हमे ये Error मिलेगा.



        

          
          data=(10,20,30.45,"Hello")
          
          min(data)
          
           


Output

        

          
      Traceback (most recent call last):
        File "< pyshell#20 >", line 1, in <module>
          min(data)
      TypeError: '<' not supported between instances of 'str' and 'int'


 


min Function हमे Error देगा | क्योंकि data के अंदर एक Element String Category का है |

इसलिए min Function Tuple पर काम नहीं कर सकता |



max()



Tuple की सबसे बड़ी वैल्यू को ढूंडनेके लिए हमे मैक्स Function इस्तेमाल करना होगा |

निचे दिए उदाहरण से हम मैक्स Function कैसे काम करता है ? यह देख सकते है |



      
      
        data=(10,20,30,40,50)
 
        max(data)

       #output 50



Tuple Functions



Tuple एक immutable data type होने के कारण हम इसके Element को Update या Delete नहीं कर सकते |

इसीलिए Tuple क्लास के केवल दो Function है – count() और index() |



Count

यह Function कौन सा Element Tuple में कितनिबार आया है , यह बताता है |



        

          animals=("Dog","Cat","Dog","Monkey","Dog")
          
        
          animals.count("Dog")
          
         #output 3 
         
          animals.count("Cat")
          
        #output  1



Previous
Next Post »

Quote

"Educationists should build the capacities of the spirit of inquiry, creativity, entrepreneurial and moral leadership among students and become their role model."

Dr. Abdual Kalam