0% found this document useful (0 votes)
9 views47 pages

TUPLE

The document provides a comprehensive overview of tuples in Python, detailing various methods for creating, accessing, and manipulating tuples. It covers topics such as single value tuples, empty tuples, tuple functions, membership operators, logical operators, and the concepts of packing and unpacking. Additionally, it includes examples and outputs for better understanding of tuple operations.

Uploaded by

pyushr013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views47 pages

TUPLE

The document provides a comprehensive overview of tuples in Python, detailing various methods for creating, accessing, and manipulating tuples. It covers topics such as single value tuples, empty tuples, tuple functions, membership operators, logical operators, and the concepts of packing and unpacking. Additionally, it includes examples and outputs for better understanding of tuple operations.

Uploaded by

pyushr013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 47

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,)
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
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:-
()
()
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))
TUPLE
OUTPUT:
(1, 2, 3, 4)
1970366848912
(1, 2, 3, 4)
1970366848912
(1, 2, 3, 4)
1970366848912
All are referring to same location
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))
TUPLE
OUTPUT:-

[10, 20, 0, 40]


1629910642048
(10, 20, 0, 40)
1627794510608
Both list and tuple are referring to different
locations.
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))

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

(10, 30, 30, 60, 50, 20)


using sorted()
[10, 20, 30, 30, 50, 60]
(10, 30, 30, 60, 50, 20)
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.
TUPLE
t=(10,20,30,40)
print(t)
a,b,c,d=t
print(a)
print(b)
print(c)
print(d)
TUPLE
OUTPUT

(10, 20, 30, 40)


10
20
30
40
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 .
TUPLE
t=(10,20,30,40)
print(t)
a,b,c,d=t
a=a+25
b=b-10
c=c+20
d=d-5
t=a,b,c,d
print(t)
TUPLE
a=100
b=200
c=300
t=a,b,c
print(t)
t=a
print(t)
t=a,
print(t)
TUPLE
OUTPUT

(100, 200, 300)


100
(100,)
TUPLE
TUPLE
TUPLE

You might also like