Tuple

 

Tuple

 

This is another Sequence Data Class available in Python. The Tuple object is immutable, that is it cannot be modified like Lists. The Tuple is enclosed within Parenthesis (). The Elements within the Tuple is separated by commas.

 

The Tuples can be

]          Null Tuple

]          Items of same Data Type

]          Items of multiple Data type

]          An Element can be another Tuple also. It is called as Nested Tuple.

 

For Tuple Data Object enclosing the values inside the brackets() are optional only. but good programming practice is to USE IT.

Few Examples

#an empty Tuple

>>> a=()

>>> print(a)

()

#Tuple of integer

>>> b=(1,2,3,4,5)

>>> print(b)

(1, 2, 3, 4, 5)

#Tuple without()

>>> b1=1,2,3,4,5

>>> print(b1," ",type(b1))

((1, 2, 3, 4, 5), ' ', <type 'tuple'>)

#Tuple of Floating point numbers

>>> c=(4.4,3.32,6.758)

>>> print(c)

(4.4, 3.32, 6.758)

#Tuple of Complex Numbers

>>> d=(3+1j,4+3j)

>>> print(d)

((3+1j), (4+3j))

#Tuple of Characters

>>> e=('a','e','i','o','u')

>>> print(e)

('a', 'e', 'i', 'o', 'u')

#Tuple of Strings

>>> f=("Ramesh","Suresh","Vignesh")

>>> print(f)

('Ramesh', 'Suresh', 'Vignesh')

#Tuple containing multiple data type

>>> g=("Magesh",53,45.325)

>>> print(g)

('Magesh', 53, 45.325)

>>> 

 

Single Element Tuple

 

When you enclose a Single object within a parenthesis, it will not create a Tuple, check it yourself in shell window.

 

t=("hai")

print(type(t))

 

This will return String Class only. Then how to create a Single Element Tuple? The Procedure for it is,

t=("hai")

 

Place a comma after the element , by this way , the Interpreter will recognize it as a Tuple. Alternatively , we can exclude the brackets and give the same.

 

t="hai",

print(type(t))

 

Individual elements of a Tuple can be accessed using the Index like in List. Index is the position of the Element and index should be always an Integer. Index value can be both Positive and Negative values also.

 

Index starts at 0 and last element index is n-1, where 'n' is the Number of Elements in the Tuple. For Negative index, the index value -1 denotes the last element. -2 is last but one and so on and -n will be the First element. If the Tuple contains 5 elements then -5 is the first element.

 

Operators

 

The Operators allowed are + and * Operators whose usage is same as in List.

#both the Tuples are combined this is called Concatenation

print(b+c)

 

#values in d are repeated n times as given

print(d*3)

 

 

Slice Operator

 

As we saw in List, Slice Operator can be used to access multiple values at a time.

 

Examples

b[2:4]   from the position of 3rd element        to 4 th element

c[2:]     from the 3rd position to the last

d[:1]     from the beginning to 2st                   element

g[:]            from beginning to last.

Methods

 

All the Methods used on List are not allowed with Tuple, as Tuple is immutable you try with the following it will generate an error only.

 

c[1]=100.27       #Not allowed as Tuple is not modifiable

print(c)

 

As already discussed, member of a Tuple can be a List also, like shown below

 

h=([1,2,3],[3,4,5])

print(h)

 

When you try to change a value in the above Tuple as shown below, this will not generate an error. This is because , since List is modifiable this is allowed.

h[0][0]=100

print(h)

 

Similarly if you try Append, Extend, Delete methods as used in List will also generate an error.

 

append()

c.append(1000)

extend

d.extend(100,200,300)

 

Both are not allowed. While deleting an element in a Tuple is not possible, but entire Tuple can be deleted using del method.

 

Syntax

 

del <Tuplename>

Example

del b[2]

This will generate error.

del b

 

This is allowed as entire Tuple is removed. After this statement if the Tuple b is accessed, it will generate an error.

 

Addition or Deletion in a Tuple

 

Though we cannot directly do that , there is an indirect way for addition or deletion of elements in a tuple. It is through overwriting the existing Tuple with new Elements1.

 

Example for adding Elements

 

t=1,2,3,4,5

print(t)

t1=6,7,8

print(t1)

t=t+t1

print(t)

Output

(1, 2, 3, 4, 5)

(6, 7, 8)

(1, 2, 3, 4, 5, 6, 7, 8)

 

Example for removing Elements

 

t=(1,3,5,7,9,11)

#To delete the 4th element 7 from the above Tuple

print(t)

T1=t[:3]

T2=t[:4]

t=T1+T2

print(t)

Output

(1, 3, 5, 7, 9, 11)

(1, 3, 5, 1, 3, 5, 7)

 

Other Methods

 

Name

Description

max()

Returns the maximum value in the Tuple

min()

Returns the minimum(lowest) value in the Tuple

sum()

Sum of the values in the Tuple

sorted()

Returns a List of Tuple values arranged in ascending or descending order

len()

Returns the count of number of occurances of a given value.

count()

Counts the number of occurances of a given value

index()

Returns the Position of the Element with in the Tuple.

 

Example

 

a=(10,5,20,8,7,1)

print(a)

sorted(a)

Output

(10, 5, 20, 8, 7, 1)

 

Packing and Unpacking of Tuple

 

The individual elements of a Tuple can be assigned to variables directly without using Index variable, this is called Unpacking a Tuple. Similarly a Tuple can be generated from multiple variables also, this is celled Packing of Tuple. This is similar to what we saw in Lists.

 

Example

 

s=(103,"GST Road","Chennai")

print(s)

print(type(s))

x,y,z=s

#=> x=103     y="GST Road1"      z="Chennai"

print(x)

print(type(x))

#integer class

print(y)

print(type(y))

#string class

print(z)

print(type(z))

#string class

Output

(103, 'GST Road', 'Chennai')

<type 'tuple'>

103

<type 'int'>

GST Road

<type 'str'>

Chennai

<type 'str'>

 

No comments:

Post a Comment