Python Tuple Methods
Python Tuple Methods
A tuple in Python is an ordered, immutable collection of items that can be of different data types. Tuples are defined by enclosing elements in parentheses () and separating them with commas.
Ex.
tup=(2,1,4,3)
print(type(tup))
Output
<class 'tuple'>
Python has two built-in methods that you can use on tuples.
Count()
Returns the number of times a specified value occurs in a tuple.
Ex.
tuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x =tuple.count(5)
print(x)
Output
2
Index()
Searches the tuple for a specified value and returns the position of where it was found.
Ex.
tuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = tuple.index(8)
print(x)
Output
3
WAP to ask the user to enter names of their 3 Favorite Movies & store them in a list.
movies=[]
movies.append(input("Enter 1st Movie:"))
movies.append(input("Enter 2nd Movie:"))
movies.append(input("Enter 3rd Movie:"))
print(movies)
Exercise
1) WAP to check if a list contains a palindrome of elements.
list1=[1,2,3]
list2=[1,2,3]
copy_list1=list1.copy()
copy_list1.reverse()
if(copy_list1==list1):
print("palindrome")
else:
print("Not palindrome")
2) WAP to count the number of students with the "A" grade in the following tuple.
["C","D","A","A","B","B","A"]
grade=["C","D","A","A","B","B","A"]
print(grade.count("A"))
3) Store the above values in a list & sort them from "A" to "D"
["C","D","A","A","B","B","A"]
grade=["C","D","A","A","B","B","A"]
grade.sort()
print(garde)
Reviewed by ADcomputercampus
on
March 04, 2026
Rating:


No comments: