1. TUPLE
Tuple can be created in many ways.
1. tuple can be created in the following manner
by writing values of different data types in the
()
T=(1,”aman”,1.5)
2. single value tuple to be created by writing
single value followed by comma.
eg:-T=(1,)
2. TUPLE
If single value tuple is written in the following
manner then it will be taking the data type of
the value which is assigned to the variable
eg:-T=(1)
T will be identified as a integer variable.
Eg-t=(“aman”)
t will be identified as a string variable
3. TUPLE
3. Empty Tuple can be created in the following
manner
t=()
print(t)
4. Empty tuple can also be created by using
tuple function
t-=tuple()
Print(t)
OUTPUT:-
()
()
4. TUPLE
5. Tuple can be created by using tuple function
and passing existing tuple as a parameter
T1=(1,2,3,4)
print(T1)
print(id(T1))
T2=tuple(T1)
print(T2)
print(id(T2))
T3=T2
print(T3)
print(id(T3))
5. TUPLE
OUTPUT:
(1, 2, 3, 4)
1970366848912
(1, 2, 3, 4)
1970366848912
(1, 2, 3, 4)
1970366848912
All are referring to same location
6. TUPLE
6. Tuple can be created by using tuple() function
and passing existing list as a parameter.
l=[10,20,0,40]
print(l)
print(id(l))
t=tuple(l)
print(t)
print(id(t))
7. TUPLE
OUTPUT:-
[10, 20, 0, 40]
1629910642048
(10, 20, 0, 40)
1627794510608
Both list and tuple are referring to different
locations.
8. TUPLE
7. Tuple can also be created by passing string as
a parameter
st="WELCOME"
t=tuple(st)
print(st)
print(id(st))
print(t)
print(id(t))
10. 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)
11. 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)
12. TUPLE
10. Nested tuple can be created in the following
manner
T=(1,2,3,(4,5,6))
Print(T[3][-1])
OUTPUT:-
6
13. 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])
14. 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)
15. 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)
17. 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.
26. 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
27. 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))
29. 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))
33. 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
34. 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))
36. 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)
38. TUPLE
• Packing and Unpacking
The process extracting values from the tuple and
assigning to different variables is known as
unpacking.
The number of variables used left to the “=“
should be equal to the length of the tuple.
41. TUPLE
PACKING is the process where individual variables to be written
to the right side of the “=“
If the programmer wants to change the values of the tuple, first
unpacking to different variables to be done then manipulate
the values as per the requirement and then pack the variable
into a tuple as shown in the example.
The id of tuple is different from the id of the tuple after
unpacking .