Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 2 — Tuple Methods

Lesson 31 of 50 in the free Python Programming notes on Siksha Sarovar, written by Rohit Jangra.

Tuple Methods

Because tuples are immutable, they support only two built-in methods:

MethodPurpose
count(x)Number of occurrences of x in the tuple
index(x)Index of the first occurrence of x
t = (1, 2, 3, 2, 4, 2)

print(t.count(2))    # 3
print(t.index(2))    # 1 (first occurrence)
# print(t.index(9))  # ValueError: 9 is not in tuple

Why so few methods?

Methods like append(), remove(), sort(), insert() do not exist for tuples because they would need to modify the tuple in place — which immutability forbids.

t = (5, 3, 1, 4)
# t.sort()          # AttributeError: 'tuple' object has no attribute 'sort'
sorted_list = sorted(t)   # use the built-in sorted() instead -> returns a list
print(sorted_list)          # [1, 3, 4, 5]

Converting between list and tuple when you need mutability

t = (1, 2, 3)
lst = list(t)       # convert to list to modify
lst.append(4)
t = tuple(lst)       # convert back to tuple
print(t)              # (1, 2, 3, 4)

Nested tuple example

students = (("Riya", 92), ("Amit", 78), ("Zoya", 85))
for name, marks in students:
    print(name, "scored", marks)