.What is tuple?What are
its basic operations and functions?
Ans:-Python tuple is used to store the sequence of immutable python
objects.Tuple is similar to lists since the value of the items stored in the
list can be changed whereas the tuple is immutable and the value of the items
stored in the tuple can not be changed.A tuple can be written as the collection
of comma-separated values enclosed with the small brackets.A tuple can be
defined as follows:-
T1=(101,”xyz”,22)
EXAMPLE:-
tuple1=(10,20,30,40,50)
print(tuple1)
count=0
for I in tuple1:
print(“tuple1[%d]=%d”%(count,i);
count=count+1
TUPLE OPERATIONS:-
The operators like
concatenation(+),repetition(*),membership(in)works in the same way as they work
with the list.
EXAMPLE:-Tuple t=(1,2,3,4,5)and Tuple t1=(6,7,8,9)are
declared.
(i)Repetition:-The repetition operator enables the tuple
elements to be repeated multiple times.
Example:- T1*2=(1,2,3,4,5,1,2,3,4,5)
(ii)Concatenation:-It concatenates the tuple mentioned on
either side of the operator.
Example:-T1+T2=(1,2,3,4,5,6,7,8,9)
(iii)Membership:-It returnstrue if a particular item exists
in the tuple otherwise false.
Example:-print(2 in T1)prints True
(iv)Iteration:-The for loop is used to iterate over the tuple
elements.
Example:-for I in T1
Print(i)
(V)Length:-It is used to get the length of the tuple.
Example:-len(T1)=5
TUPLE FUNCTIONS:-
(i)cmp(tuple1,tuple2):-It compares two tuples and returns
true if tuple1 is greater than tuple2 otherwise false.
(ii)len(tuple):-It calculates the length of the tuple.
(iii)max(tuple):-It returns the maximum element of the tuple.
(iv)min(tuple):-It returns the minimum element of the tuple.
(v)tuple(seq):-It converts the specified sequence to the
tuple.