Consider the following tuples, tuple1 and tuple2:
tuple1 = (23,1,45,67,45,9,55,45)
tuple2 = (100,200)
Find the output of the following statements:
i. print(tuple1.index(45))
ii. print(tuple1.count(45))
iii. print(tuple1 + tuple2)
iv. print(len(tuple2))
v. print(max(tuple1))
vi print(min(tuple1))
vii. print(sum(tuple2))
viii. p r i n t ( s o r t e d ( t u p l e 1 ) )
print(tuple1)
Answers
Explanation:
i. The 'index()' function returns the index of the first occurrence of the element in a tuple.
OUTPUT:
2
ii. The 'count()' function returns the numbers of times the given element appears in the tuple.
OUTPUT:
3
iii. '+' operator concatenate the two tuples.
OUTPUT:
(23, 1, 45, 67, 45, 9, 55, 45, 100, 200)
iv. The 'len()' function returns the number of elements in the given tuple.
OUTPUT:
2
v. The 'max()' function returns the largest element of the tuple.
OUTPUT:
67
vi. The 'min()' function returns the smallest element of the tuple.
OUTPUT:
1
vii. The 'sum()' function returns the sum of all the elements of the tuple.
OUTPUT:
300
viii. The 'sorted()' function takes element in the tuple and return a new sorted list. It doesn’t make any changes to the original tuple. Hence, print(tuple1) will print the original tuple1 i.e. (23, 1, 45, 67, 45, 9, 55, 45)
OUTPUT:
[1, 9, 23, 45, 45, 45, 55, 67]
(23, 1, 45, 67, 45, 9, 55, 45)
Answer:
python program
Explanation:. The 'index()' function returns the index of the first occurrence of the element in a tuple.
OUTPUT:
2
ii. The 'count()' function returns the numbers of times the given element appears in the tuple.
OUTPUT:
3
iii. '+' operator concatenate the two tuples.
OUTPUT:
(23, 1, 45, 67, 45, 9, 55, 45, 100, 200)
iv. The 'len()' function returns the number of elements in the given tuple.
OUTPUT:
2
v. The 'max()' function returns the largest element of the tuple.
OUTPUT:
67
vi. The 'min()' function returns the smallest element of the tuple.
OUTPUT:
1
vii. The 'sum()' function returns the sum of all the elements of the tuple.
OUTPUT:
300
viii. The 'sorted()' function takes element in the tuple and return a new sorted list. It doesn’t make any changes to the original tuple. Hence, print(tuple1) will print the original tuple1 i.e. (23, 1, 45, 67, 45, 9, 55, 45)
OUTPUT:
[1, 9, 23, 45, 45, 45, 55, 67]
(23, 1, 45, 67, 45, 9, 55, 45)