Wednesday, 23 March 2016

Tuple in python with code examples

Tuple in python with code examples:

Tuples are sequences, just like lists in python. A tuple is a sequence of immutable objects.

Differences between the touple and list:


  • ·       The tuples cannot be changed unlike list.
  • ·        Tuples use parentheses, and lists use square brackets.

The below are the examples of the tuples:

EX_tup1 = ('String1', 'String2', 1919, 2100);

EX_tup2 = (1, 2, 3, 4, 5, 6, 7 );

EX_tup3 = "a", "b", "c", "d", "e", "f";


A tuple creating is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also.
Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.
The empty tuple is written as two parentheses containing nothing
EX_tup1 = ()
To write a tuple containing a single value you have to include a comma, even though there is only one value

EX_tup
= (50,);

Accessing Values in Tuples:

Using the square brackets for slicing along with the index or indices to obtain value available at that index.

import sys
        
if __name__ == '__main__':
   
    EX_tup1 = ('String1', 'String2', 1997, 2000);
    EX_tup2 = (1, 2, 3, 4, 5, 6, 7 );

    print " EX_tup1[0]: ", EX_tup1[0]
    print " EX_tup2[1:5]: ", EX_tup2[1:5]

The output of the above code is

EX_tup1[0]:  String1
EX_tup2[1:5]:  (2, 3, 4, 5)


Tuples Updating in Python:

Tuples are immutable which means you cannot update or change the values of tuple elements. You are able to take portions of existing tuples to create new tuples as the following example demonstrates −


import sys
        
if __name__ == '__main__':
   
    EX_tup1 = ('String1', 'String2', 1997, 2000);
    EX_tup2 = (1, 2, 3, 4, 5, 6, 7 );

    print " Result ", EX_tup1 + EX_tup2
   

The result of the above code is

Result  ('String1', 'String2', 1997,
2000, 1, 2, 3, 4, 5, 6, 7) 

How to Delete Tuple Elements:

Deleting of individual elements in the list is not possible. Using the
del
statement we can remove entire tuple
The bwloe is the sample code that is used to delete the tuple values

if __name__ == '__main__':
   
    EX_lst11 = ['String1', 'String2', 1997, 2000]
    EX_lst12 = [1, 2, 3, 4, 5, 6, 7]

    print " Result ", EX_lst11 + EX_lst12
    del EX_lst12
    print EX_lst12
    

The result of the above code would be

Result  ['String1', 'String2', 1997, 2000, 1, 2, 3, 4, 5, 6, 7]
Traceback (most recent call last):
     print EX_lst12
NameError: name 'EX_lst12' is not defined

Basic Tuples Operations in python:

Tuples support concatenation and repetition here too, except that the result is a new tuple.tuples respond to all of the general sequence operations.
The below list describes the list of tuple operations that the python supports

if __name__ == '__main__':  
    EX_tup1 = ('String1', 'String2', 1997, 2000);
    EX_tup2 = (1, 2, 3, 4, 5, 6, 7 );

    print " Concatination :", EX_tup1 + EX_tup2
    print "Length:" , len(EX_tup1)
    print "Repetition:" , (EX_tup2) * 4
    print "Membership" , 3 in EX_tup2
    for i in EX_tup2:
        print i   

The result of the above code is

Concatination : ('String1', 'String2', 1997, 2000, 1, 2, 3, 4, 5, 6, 7)
Length: 4
Repetition: (1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7)
Membership True
1
2
3
4
5
6
7

Indexing, Slicing, and Matrixes of tuples in python:

As of we know that tuples are sequences, indexing and slicing works.
Let us try to understand the below example

if __name__ == '__main__':
   
    EX_tup1 = ('String1', 'String2', 'String3', 'String4');
    EX_tup2 = (1, 2, 3, 4, 5, 6, 7 );

    print " Offsets start at zero:", EX_tup1[2]
    print "Negative: count from the right:" , EX_tup1[-2]
    print "Repetition:" , (EX_tup2) * 4
    print "Slicing fetches sections" , EX_tup2[1:]

The output of the above program is

Offsets start at zero: String3
Negative: count from the right: String3
Repetition: (1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7)
Slicing fetches sections (2, 3, 4, 5, 6, 7)

Built-in Tuple Functions:

The below are the built in function of tuple

if __name__ == '__main__':
   
    lst = ['String1', 'String2', 1997, 2000]
    EX_tup2 = (1, 2, 3, 4, 5, 6, 7 );

    print "Length:" , len(EX_tup2)
    print "Minimum value:" , min(EX_tup2)   
    print "Maximum value:" , max(EX_tup2)
    print "Convert list to tuple:" ,   tuple(lst)

The output of the above program is
Length: 7
Minimum value: 1
Maximum value: 7
Convert list to tuple: ('String1', 'String2', 1997, 2000)


No comments: