Tuples are immutable
§ There
is a major difference between lists and tuples in python is that tuples are
immutable i.e. tuple can't be modifying once they have to initialized that is
add or modify items from the tuple is
not allowed once the tuple is initialized.
tuple1=(10,20,30,40,50)
tuple1[0]=60
print(tuple1)
tuple1[0] =60
TypeError: 'tuple' object does not support item assignment
Important Ponts:
§
If
all of the elements present in the tuple are of the immutable type, then
no
element of the tuple can be changed. but if any element is of the
mutable type
like list, then that element of a tuple can be changed.
tuple1=(10,20,30,40,50)
tuple1[0]=60
tuple1[0]=60
TypeError: 'tuple' object does not support item assignment
§ We cannot change this tuple because all the elements
present in it are of an immutable type.
tuple1= (10,20,30, [40,50])
tuple1[3][0] =60
print(tuple1) #The
Output (10, 20, 30, [60, 50])
§ In this tuple there is a list object at 3 positions which
is immutable type, so we can change the 3 elements of the tuple.
tuple1=(10,20,30,[40,50])
tuple1[3].append(60)
print(tuple1)
Similarly, tuples don't have. append and. extend methods as list does.
tuple1=(10,20,30,40,50)
tuple2=tuple1.append(60)
print(tuple2)
tuple2=tuple1.append(60)
AttributeError: 'tuple' object has no attribute 'append'
tuple1=(10,20,30)
tuple2=(40,50,60)
tuple3=tuple1.extend(tuple2)
print(tuple3)
tuple3=tuple1.extend(tuple2)
AttributeError: 'tuple' object has no
attribute 'extend'
Deleting a Tuple in Python
§ We already
discussed, that tuple elements are immutable i.e we cannot change the elements
in a tuple. That also means we cannot delete or remove items from a tuple.
§ However, deleting
entire tuple is possible using the keyword del.
my_tuple = (10,20,30,40,50)
del my_tuple[3]
print(my_tuple)
del my_tuple[3]
TypeError: 'tuple' object
doesn't support item deletion
my_tuple = (10,20,30,40,50)
# Can delete an entire
tuple
del my_tuple
# NameError: name 'my_tuple' is not defined
print(my_tuple)
print(my_tuple)
NameError: name
'my_tuple' is not defined
Tuple
Method
Count()
method in Tuple
§ count () method
tell how many times an element is occurrence in a tuple. So, count () method
returns the occurrence of an element in a list.
§ The count () method
takes a single parameter.
tuple.count(element)
element - element
whose count is to be found
Example
1: counting an element in a tuple
tuple1=[10,20,10,10,30,20]
n=int(input('enter element'))
c=tuple1.count(10)
print(n,' occur' ,c,'times')
Example
2: counting a list in a tuple
tuple1=([10,20],[30,40],[10,20],[50,60])
list1=eval(input('enter list for count'))
c=tuple1.count(list1)
print(list1,' occur' ,c,'times')
Tuple
Length
§ The function len
returns the total length of the tuple
tuple1= (10,20,30,40,50)
print('Length of Tuple=',len(tuple1))#The Output Length of Tuple=
5
Max
of a tuple
§ The
function max returns item from the tuple with the max value
tuple1= (10,20,30,40,50)
print ('Max of Tuple= ‘, max(tuple1)) #Max
of Tuple= 50
Min of a tuple
§ The
function min returns the item from the tuple with the min value.
tuple1= (10,20,30,40,50)
print ('Min of Tuple= ‘, min(tuple1)) #Min of Tuple= 50
Convert a list into tuple
§ The
built-in function tuple converts a list into a tuple.
list1=[10,20,30,40,50]
tuple1=tuple(list1)
print(list1)
print(tuple1)
#The Output
[10, 20, 30, 40, 50]
(10, 20, 30, 40, 50)
Slicing of a Tuple
(selecting parts of lists)
§ Slicing
of a Tuple means selecting a part of a tuple.
tuple[beginindex:endindex:step]
§ from begin index to end-1 index and every time update by step
§ default value for begin index is zero.
§ default value for end index is length of string.
tuple[begin:end:step]
§ step value can be either +ve or -ve.
§ if +ve then it should be forward direction (Left to Right).
§ If -ve then it should be backward direction (Right to Left).
§ If +ve forward direction from begin to end-1.
§ if -ve backward direction from begin to end+1.
Selecting a subtuple from a tuple.
tuple1=(10,20,30,40,50,60,70)
print(tuple1[1:4])#The
OutPut (20, 30, 4)
print(tuple1[3:7]);#The
OutPut (40, 50, 60, 70)
v If
you omit the first index, the slice starts at the beginning of the tuple. Thus,
tuple[:m] and tuple[0:m] are equivalent:
tuple[:endindex]
tuple1=(10,20,30,40,50,60,70)
print(tuple1[:2])#The OutPut (10, 20)
print(tuple1[:3]);#The OutPut (10, 20, 30)
v if
you omit the second index as in s[n:], the slice extends from the first index
through the end of the list. This is a nice, concise alternative to the more
cumbersome s[n:len(s)]:
tuple[beginindex:]
tuple1=(10,20,30,40,50,60,70)
print(tuple1[5:])#The OutPut (60, 70)
print(tuple1[6:]);#The OutPut (70,)
v If
you Omitting both indices return the original tuple.
tuple[:]
tuple1=(10,20,30,40,50,60,70)
print(tuple1[:])#The OutPut(10, 20, 30, 40, 50, 60, 70)
v If
the first index in a slice is greater than or equal to the second index, Python
returns an empty tuple.
tuple1=(10,20,30,40,50,60,70)
print(tuple1[2:2])#The OutPut ()
print(tuple1[4:2]);#The OutPut ()
v
Reversing a tuple with
slicing
a = (10, 20, 30, 40, 50)
# steps through the tuple backwards (step=-1)
b = a[::-1]
print('Original List=',a)
print('Reverse List= ',b)
Appending an
element in tuple
Suppose we have a tuple
i.e.
my_tuple=(10,20,30,40)
§ If we want to
append the value at the end of the tuple, then first we have to make copy of
existing tuple and then add new element to it using + operator.
#Appending 50 at
the end of tuple
my_tuple=my_tuple+(50,)
§ And then we will put the address of the newly created tuple in
the
original reference.
§ hence it will give
an effect that new element is added to existing tuple.
print(my_tuple)
Program
my_tuple=(10,20,30,40)
print(my_tuple)#The
Output (10, 20, 30, 40)
#Appending 50 at the end of tuple
my_tuple=my_tuple+(50,)
print(my_tuple)#The Output (10, 20, 30, 40, 50)
§ If
we want to insert an element at a specific index position in the existing tuple
then
first off all we have to create a new tuple by slicing the existing tuple
and copying
contents from it.
# Create a tuple
tuple1 = (10,20,30,50)
§ If
we want to insert an element at index n in this tuple, then we will create two
sliced
copies of existing tuple from (0 to n) and (n to end) i.e.
# Sliced copy containing
elements from 0 to n-1
tupleObj[ : n]
# Sliced copy containing
elements from n to end
tupleObj[n : ]
# Create a tuple
my_tuple = (10,20,30,50)
n = 3
# Insert 40 in tuple
at index 2
my_tuple = my_tuple[ :
n ] + (40 ,) + my_tuple[n : ]
print(my_tuple)#The
Output (10, 20, 30, 40, 50)
Modify / Replace
the element at specific index in tuple
§
If
we want to Modify / Replace the element at a specific index position in the
existing tuple we will slice the tuple from (0 to n-1) and (n+1 to end) i.e.
tuple=tuple [: n] +(value,) +tuple[n+1:]
# Create a tuple
my_tuple = (10,20,30,40,50)
n = 3
# Insert 40 in tuple at index 2
my_tuple = my_tuple[ : n ] + (40 ,) +
my_tuple[n+1 : ]
print(my_tuple)#The Output (10, 20, 30, 40, 50)
§ If
we want to Modify / Replace the element at a specific index position in the
existing tuple we will slice the tuple from (0 to n-1) and (n+1 to end) i.e.
tuple=tuple
[: n] +tuple[n+1:]
# Create a tuple
my_tuple = (10,20,30,50)
n = 3
# Insert 40 in tuple
at index 2
my_tuple = my_tuple[ :
n ] + my_tuple[n+1 : ]
print(my_tuple)#The
Output (10, 20, 30, 50)
Post a Comment