The Python tuples are sequences that are
used to store a tuple of values of any type.
•Tuples are immutable.
•Tuple is a type of sequence like string
and list but it differs from them in way that lists are mutable but strings and
tuples are immutable
Creating and Accessing Tuples
Tuples
are depicted through parenthesis.
()
Empty tuple
(1,2,3)
Tuple of integers
(2,3.5,6,7.5)
Tuple of integer and float
(‘x’,’y’,’z’)
Tuples of characters
Creating Tuples
1.Empty Tuple
>>>
t=tuple()
>>>
t
()
2.
Single Element
>>>
t=(1)
>>>
t
1
>>>
t=3, #
to construct a tuple with one element just add comma
>>>
t
(3,)
3.
Long Tuples
>>>
t1=(11,22,33,44,55,66,77,88)
>>> t1
(11, 22, 33, 44, 55, 66, 77, 88)
>>> print(t1)
(11, 22, 33, 44, 55, 66, 77, 88)
4. Nested Tuples:
t=(11,22,(33,44),55)
>>>
t
(11,
22, (33, 44), 55)
Creating Tuples from Existing Sequences
T1=tuple(<sequence>)
>>>
t1=tuple('python')#Passing String
>>>
t1
('p',
'y', 't', 'h', 'o', 'n')
>>>
l1=['r','a','m','l','a','l']
#Passing list
>>>
t2=tuple(l1)
>>>
t2
('r',
'a', 'm', 'l', 'a', 'l')
>>> t3=tuple(input("Enter
tuple"))
Enter tuple98765
>>> t3
('9', '8', '7', '6', '5')
tuple=eval(input("Enter
tuple"))
Enter
tuple98765
>>>
tuple
98765
Simlarity with list Accessing Tuple
Tuples
are immutable(non-editable) sequence having a progression of elements. Thus,
other than editing items, you can do all that you can do with list.
1-
Tuple elements are indexed just like list.
2-
Tuple[i] will give element at ith
index.
3-
Tuple[i:j]
return new tuple, containing object from I to j excluding j
4-
Tuple[i:j:n]
return new tuple, containing every nth
element from i to j excluding j
Accessing individual elements of Tuple
>>> elements=('a','b','c','d')
>>> elements[0]
'a'
>>> elements[-1]
'd‘
While
accessing tuple elements, if you pass in a negative index, Python adds
the length of the tuple
to the index to get elements forward index.
T[-5+6]=T[1]
DIFFERENCE FROM LIST
L[1]=element #VALID for List
T[1]=element #INVALID for Tuple(immutable)
>>>
l1=['a','e','i','o','u']
>>>
T1=('a','b','c','d')
>>>
l1[1]='z'
>>>
T1[1]='x'
Traceback
(most recent call last):
File "<pyshell#6>", line 1,
in <module>
T1[1]='x'
TypeError:
'tuple' object does not support item assignment
Traversing a Tuple
a=0
t=('t','u','p','l','e')
for a
in range(5):
print(t[a])
t=('all',
'that', 'you','need','is','a','hammer')
length=len(t)
for i
in range(length):
print(t[i])
Tuple Operations
1.Joining Tuples
>>>
t1=(1,2,3)
>>>
t2=(4,5,6)
>>>
t1+t2
(1,
2, 3, 4, 5, 6)
The +
operator when used with tuple requires that both the operands must be op type
tuple.
Tuple+number
Tuple+complex
number ERROR
Tuple+string
Tuple+list
>>>t1+(3) # error
Reason:
a single value in () is treated as single value not as tuple.
Where
as (3,) are example of tuples with single element.
So
>>>t1+(3,)
# valid
Replicating Tuples
>>>t2=(2,3,4)
>>>t2*2
(2,3,4,2,3,4)
Slicing the Tuples
Slice=T[start:stop:step]
>>>
t1=(11,22,33,44,55,66,77)
>>> slice=t1[0:7:1]
>>> slice
(11, 22, 33, 44, 55, 66, 77)
>>> slice1=t1[0:7:2]
>>>
slice1
(11,
33, 55, 77)
>>> slice2=t1[::]
>>> slice2
(11, 22, 33, 44, 55, 66, 77)
>>>
slice3=t1[]
SyntaxError:
invalid syntax
>>>
slice3=t1[:]
>>>
slice3
(11,
22, 33, 44, 55, 66, 77)
At
least one colon is required to create slice of a tuple.
Comparing Tuples
>>>
t1=(11,22,33)
>>>
t2=(11,22,33)
>>>
t1==t2
True
>>>
t3=(44,55,22)
>>>
t3==t1
False
>>>
t3=t2
>>>
t3==t1
True
Unpacking Tuples
•Forming a tuple from individual values is
called packing and creating individual values from tuple’s elements is called
unpacking.
>>> t1=(11,22,'abc','xyz')
#Packing
>>> a,b,c,d=t1 #Unpacking
>>> print(a,b,c,d)
11 22 abc xyz
Deleting Tuples
The
del statement of Python elements but as we know that tuples are immutable,
hence following statement will show error:
>>>
t1=(11,22,33,44,55)
>>>
del t1[0]
Traceback
(most recent call last):
File "<pyshell#11>", line 1,
in <module>
del t1[0]
TypeError:
'tuple' object doesn't support item deletion
But
you can delete a complete tuple with del() function.
>>>
t1=(11,22,33,44,55)
>>>
del t1
>>>
t1
Traceback
(most recent call last):
File "<pyshell#2>", line 1,
in <module>
t1
NameError:
name 't1' is not defined
Tuple Functions
1.len()
>>>
t1=(11,22,33,44,55)
>>>
len(t1)
5
2.max()
3.min()
4.index()
>>>
t1=(11,33,55,77,99)
>>>
t1.index(33)
1
5.
count()
>>>
t2=(11,22,33,44,55,11,33,55)
>>>
t2.count(11)
2
6.
tuple()
•Creating empty tuple
>>>
tuple()
()
•Creating tuple from string
>> t=tuple("xyz")
>>> t
('x', 'y', 'z')
Miscellaneous
Indirectly
modifying Tuple
1.Using tuple Unpacking
>>> t1=(11,22,33,44)
>>> x,y,z,w=t1
>>> x=100
>>> t1=(x,y,z,w)
>>> t1
(100, 22, 33, 44)
2. Using
constructor function of lists and tuple
>>>
t1=("Aliganj",10000,10,"Lucknow")
>>>
t1
('Aliganj',
10000, 10, 'Lucknow')
>>>
L1=list(t1) #Covert
tuple to list
>>>
L1
['Aliganj',
10000, 10, 'Lucknow']
>>>
L1[1]=50000 #
Make the changes through list
>>>
L1
['Aliganj',
50000, 10, 'Lucknow']
>>>
t1=tuple(L1) # create
new tuple with modified list
>>>
t1
('Aliganj',
50000, 10, 'Lucknow')
Comments
Post a Comment