TUPLE IN PYTHON
![]() |
Q.What is Tuple?
· Tuple is immutable data type ,which is used to store some values and it represent by () paranthesis.
Let understand through examples that how to write values in tuple:
Tuple=(1,2,3,4,5)
T=(“abcd”,”bcd”)
Now we indexing the values in the tuple
Let tuple=(1,2,3,4,5)
In tuple index value start from 0 .So
o Index value of 1 is 0
o Index value of 2 is 1
Index value of 3 is 2
o Index value of 4 is 3
o Index value of 5 is 4
Here we have a function to find index value of any element in the tuple
Let understand through example:\
Testcase1:
Input
t=(1,2,3)
index=1
Output:
2
>>>3
>>>7
And the length of tuple is equal to total number of elements in the tuple
So the length of given tuple (1,2,3,4,5) is 5.
To find length of any tuple we can use a function i.e len() function
Let understand through example
Testcase1:
Input:
T=(1,2,30)
Output:
3
Testcases2:
Q.HOW TO SPLIT THE TUPLE?
· In tuple we cannot split the tuple datatype like list data type.
· So only we can partition the tuple it means we will understand through some example:
Testcases1:
>>> ('abcd ', 'is', ' alphabet')
Partition() function will split the string from the given argument .And after partition of the given tuple its length will be always 3.
Q.How to select a number from a tuple in Python?
o #let if you want to do any changes in tuple by selecting any element .So to do this you have to use in build functions.
o Before understand this you have to know about membership operators and replication operator.
o So understand through any example:
1)Membership operator means to join elements two or three or many tuple in a single tuple .
Testing:t=(1,2,3,4)
Ouput
>>> (1, 2, 3, 4, 2, 34)
2)Replication-means you can replicate the values in the tuple many times that you want understand by examples
Ouput:
(2,3,9,2,3,9)
§ Now we are going to select the numbers and to do operation
If you have given any tuple and you want to find maximum value in the tuple
If t=(7,8,98)
So in the given tuple maximum value is 90.
This operation we can do using in build function i.e max() function
So let understand by program
>>>4
Similarily if you want to check minimum value in the given tuple so min() function we can use to find minimum value in the given tuple.
T=(6,7,2,3,5,667,8,89,8,9,7,5,9)
So miminmum value in T is 2
Testing:1
t=(3,2,34,4,78)
print(min(t))
>>>2
Now if you want to find the sum of the given tuple we can use the function i.e sum() function
Let understand through examples
T1=(8,7)
So the sum will be 8+7=15
Understand using function:
Output>>>16
If you want to make your tuple element in ascending order then you have to use sorted() function.
Let understand by examples :
T=(9,6,12,8,9)
So after sorting the list
>>>[6,8,9,9,12]
So similarily using function
T=(0,98,9)
print(sorted(T))
Output:
>>>[0,9,98]
Now we will do some programs on Tuple
"""
Q.Write a program to remove duplicate elements from the tuple
Example:
T=(4,6,7,6,4)
so after removing duplicate elements
output will be
(4,6,7)
"""
Tuple=eval(input("Enter a elements in the tuple:"))
T=list(Tuple)
#here we have changed the data type of tuple into list because in tuple there is no allow
print("Before removing duplicate elements from the tuple is",T)
t=[] #assume empty list and input all element without repeatation of the elements
#Take a loop and it goes upto length of list -1
for ele in range(0,len(T)):
#Then test the condition using membership operator
if ele not in t:
#if condition will be true then append that value
t.append(T[ele])
print("After removing duplicate elements in the tuple is:",tuple(t))
#Whatever is written in in triple quotes (""" """) inside it which is comment ,so that you analyse the program .And whatever is statement written after (#) Hastag sign that is also comment it woulb be only in that line(one line)
No comments: