Python3 Tuples DS

Python Data Structures : Tuple 

Data Structures : Tuple 

Tuples

   ->A tuple is similar to list.
   ->The difference between the two is that we can't change the elements of the tuple(immutable) once it is assigned whereas in the list, elements can be changed.

Tuple Creation :

Eg:
             #empty tuple
       t=( )
       
       #tuple having integers
       t=(1,2,3)
       print(t)
 
       #tuple with mixed data types
       t=(1,'rama',2.5,'a')
       print(t)

       #nested tuple
       t=(1,(1,2),[1,'abc',3])
       print(t)

output:: (1,2,3)
             (1,'rama',2.5,'a')
             (1,(1,2),[1,'abc',3])

       #only parentheses is not enough
           t=('raja')
           type(t)

output:: str

       #need a comma at the end
            t=('raju',)
            type(t)

 output:: tuple

       #paranthesis is optional 
             t="raja",
             print(type(t))
             print(t)

 output::    <class 'tuple' >
                   ('raja',) 
 
 **   comma must mention at the end ,if the tuple has only single element. If more than one elements are present ,no need to mention comma at the end.

Accessing Elements in Tuple :

Eg:
       t=['ram' ,'raja', 'rishi','yuva']
       print(t[3])
       print(t[-1])

output::  yuva
                yuva
 
Eg:
       #nested tuple 
       t=('AB',('ramu', 'ravi' ,'raju')
       print(t[1])
       print(t[1][1])

output:: ('ramu','ravi','raju')
               ravi

Eg:
       #slicing
       t=(1,2,3,4,5,6)
       print(t[1:4])
       print(t[:-2])
       print(t[:])

output:: (2,3,4)
               (1,2,3,4)
               (1,2,3,4,5,6)

Changing a Tuple :

             Unlike lists, tuples are immutable

       This means that elements of a tuple can't be changed once it has been assigned. But, if the element is itself a mutable datatype like list, it nested items can be changed.

Eg:
       #creating tuple
       t=(1,2,3,4,[5,6,7])
       t[2]='x'
      
 output:  ---------------------------------------------------------------------
                --
                 TypeError
                 TypeError: 'tuple' object does not support item assignment
   
Eg:
       t[4][1]='ram'
       print(t)
 
output:: (1,2,3,4,[5,'ram',7])

Eg:
       #concatinating tuples
       t=(1,2,3)+(4,5,6)
       print(t)

output::  (1,2,3,4,5,6)

       #repeat the elements in a tuple for a given number of times using the * operator
       t=(('ram',)*4)
       print(t)

output::  ('ram','ram','ram','ram')

Tuple Deletion :
 
       #we cannot change the elements in a tuple.
       #That also means we cannot delete or remove items from a tuple.

       #delete entire tuple using del keyword
       t=(1,2,3,4,5,6)
       #delete entire tuple
       del t


Tuple Count :

Eg:
       t=(1,3,2,4,2,2,3)
       #get the frequency of particular element appears in a tuple
       t.count(2)

output:   3

 Tuple Index :

Eg:
       t=(1,2,3,1,3,3,4,1)
       print(t.index(3))      #return index of the first element is equal to 3

output:  2     
  
Tuple Membership :

      #test if an item exists in a tuple or not , using the keyword in.
       t=(1,2,3,4,5)
       print(3 in t)
       print(6 in t)
 
 output:  True
                False

Built in Functions

Tuple Length :

Eg: 
            t=(1,2,3,4,5,6)
            print(len(t))    
 
output:   6

Tuple Sort :

       #take elements in the tuple and return a new sorted list   
       #(does not sort the tuple itself)

Eg:
       t=(3,1,2,5,4)

       new_t=sorted(t)
       print(new_t)

output:   [1,2,3,4,5]

Eg:
       t=(2,3,1,4)
       print(max(t))
       print(min(t))
       print(sum(t))

output::     4
                   1
                   10






Comments

Popular posts from this blog

Python3 Function Arguments

Python3 File Handling

Python3