TUPLE
TUPLE
l=[10,20,0,40]
print(l)
print(id(l))
t=tuple(l)
print(t)
print(id(t))
TUPLE
OUTPUT:-
7
TUPLE
OUTPUT:-
WELCOME
3004542942832
('W', 'E', 'L', 'C', 'O', 'M', 'E')
3004542482368
TUPLE
8. Tuple can be created by using the eval
function.
t=eval(input ("accept values into the tuple"))
print(t)
Output:
accept values into the tuple(1,2,3,4,5)
(1, 2, 3, 4, 5)
TUPLE
9. Tuple can also be created by using
“replication operator” in the following
manner:
T=(1,2,3)
T1=T*3
print(T1)
OUTPUT:-
(1,2,3,1,2,3,1,2,3)
TUPLE
10. Nested tuple can be created in the following
manner
T=(1,2,3,(4,5,6))
Print(T[3][-1])
OUTPUT:-
6
TUPLE
11. List can be inside the nested tuple
T-(11,22,33,[1,2,3])
OUTPUT:-
(11, 22, 33,[1,2,3])
t=(11,22,33,[1,2,3])
print(t)
t[3][-1]=33
print(t)
OUTPUT:-
(11, 22, 33,[1,2,33])
TUPLE
12. New tuple can be created by extracting the
slice from the existing tuple.
t=(11,22,33,44,55)
nt=t[2:20]
print(t)
print(nt)
Output:-
(11, 22, 33, 44, 55)
(33, 44, 55)
TUPLE
13. New tuple can be created by by joining two
tuples using concatenation ‘+’
t1=(11,22,33,44,55)
t2=(66,77)
t3=t1+t2
Print(t1)
Print(t2)
Print(t3)
TUPLE
OUTPUT:
(11,22,33,44,55)
(66,77)
(11,22,33,44,55,66,77)
TUPLE
Accessing an individual element can be done in
the following manner
T=(11,22,33,44,55)
Print(T[2])
Output:
33
In the case of tuple, individual item assignment
is not possible.
T[2]=222---is wrong.
TUPLE
t=(11,22,33,[1,2,3])
print(t)
t[3][-1]=33
print(t)
OUTPUT:-
(11,22,33,[1,2,3])
(11,22,33,[1,2,33])
TUPLE
MEMBERSHIP OPERATORS
t=(11,22,33,[55,66,77],[88,99],100)
print (55 in t)
print(22 in t)
print([55,66,77] in t)
print([55,66,777] in t)
print ([55,66,77] not in t)
TUPLE
OUTPUT:-
False
True
True
False
False
TUPLE
LOGICAL OPERATORS
t=(11,22,33,[55,66,77],[88,99],100)
print(t[0] and 100)
print (0 or t[3])
print(t and 0)
TUPLE
OUTPUT:-
100
[55, 66, 77]
0
TUPLE
Tuple slices:-
t=(1,"aman",20,12.5,89.5,"x",[10,23,45],89)
print(t[2:5:1])
print(t[0: :])
print(t[6: :1])
print(t[ :len(t):])
print(t[ :(len(t)+20):])
print(t[0:34:1])
print(t[0:56:1])
TUPLE
• OUTPUT:-
(20, 12.5, 89.5)
(1, 'aman', 20, 12.5, 89.5, 'x', [10, 23, 45], 89)
([10, 23, 45], 89)
(1, 'aman', 20, 12.5, 89.5, 'x', [10, 23, 45], 89)
(1, 'aman', 20, 12.5, 89.5, 'x', [10, 23, 45], 89)
(1, 'aman', 20, 12.5, 89.5, 'x', [10, 23, 45], 89)
(1, 'aman', 20, 12.5, 89.5, 'x', [10, 23, 45], 89)
TUPLE
TUPLE-FUNCTIONS
1. len()- is used to get the length of the tuple.
t=(11,22,33,44)
L=len(t)
Print(L)
OUTPUT:
4
TUPLE
2.sum(t) –is used to get total of all the values
present in the tuple
t=(10,20,30,40,50,30)
print(t)
print("sum")
print(sum(t))
OUTPUT:-
SUM
180
TUPLE
3. TUPLE.COUNT(VALUE)- this function will find
out how many times the value exists in the
tuple and returns the value
t=(10,20,30,40,50,30)
print(t)
print("count")
print(t.count(30))
print(t.count(70))
print(t.count(80))
TUPLE
Output:-
count
2
0
0
TUPLE
4. tuple.index(value)-This function is used to
find out the index of the value which is passed
as a parameter.
If the value is not present in the tuple then value
error will be thrown
t=(10,20,30,40,50,30)
print(t.index(50))
#print(t.index(80))
TUPLE
OUTPUT
VALUE ERROR
TUPLE
• t1=(2,3,4,5)
• t2=(1,2,3)
• t3=t1+t2
• print(t3)
• print(t3.index(3,0))
• print(t3)
TUPLE
OUTPUT
(2, 3, 4, 5, 1, 2, 3)
1
(2, 3, 4, 5, 1, 2, 3)
TUPLE
5. del() function can be used to delete the tuple
T=(10,20,30,40,50)
Print(T)
Del T
(10,20,30,40,50)
T not defined
TUPLE
6.max(tuple)-This function extracts the maximum
value out of the all values of the tuple
t=(111,444,222,666,555)
print(t)
Print(“Max()”)
print(max(t))
7.min(t)-This function extracts the minimum
value out of the all values of the tuple
t=(111,444,222,666,555)
Print(“min()”)
print(min(t))
TUPLE
Output:
(111,444,222,666,555)
Max()
666
Min()
111
TUPLE
8. sorted(tuple)-This function will return sorted values of the
tuple without making any change in the values of the tuple.
The sorted values will be returned in the form of a list.
t=(10,30,30,60,50,20)
print(t)
print("using sorted()")
t1=sorted(t)
print(t1)
print(t)
TUPLE
OUTPUT