Skip to content
Home » Tuples

Tuples

Here is a clear, complete, and exam-friendly explanation of Tuples in Python, perfect for BCA/MCA/B.Tech students.


Tuples in Python

A tuple in Python is an ordered, immutable (unchangeable) collection of items.
Tuples are similar to lists, but unlike lists, you cannot modify their elements once created.

Tuples are used when:

  • You want to store fixed data
  • Data should not be changed accidentally
  • You need faster performance (tuples are faster than lists)

1. Creating a Tuple

Tuples are created using parentheses ( ) or commas.

Examples:

t1 = (10, 20, 30)        # Tuple of integers
t2 = ("apple", "banana") # Tuple of strings
t3 = (10, "Python", 3.14)
t4 = ()                  # Empty tuple
t5 = (5,)                # Single-element tuple (comma required!)

Without parentheses:

t6 = 1, 2, 3

2. Accessing Tuple Elements

Use indexing:

fruits = ("apple", "banana", "mango")
print(fruits[0])   # apple
print(fruits[1])   # banana

Negative indexing:

print(fruits[-1])  # mango

3. Slicing a Tuple

Tuples support slicing just like lists.

t = (1, 2, 3, 4, 5, 6)

print(t[1:4])    # (2, 3, 4)
print(t[:3])     # (1, 2, 3)
print(t[::2])    # (1, 3, 5)

4. Tuples Are Immutable

Once created, elements cannot be changed, added, or removed.

Example:

t = (1, 2, 3)
t[1] = 20     # ❌ Error (tuples cannot be modified)

5. Updating Tuple (Workaround)

Convert to list → modify → convert back:

t = (1, 2, 3)
lst = list(t)
lst[1] = 20
t = tuple(lst)
print(t)

6. Tuple Methods

Tuples have only two built-in methods:

1. count(x)

Returns how many times x appears.

t = (1, 2, 2, 3)
print(t.count(2))   # 2

2. index(x)

Returns the index of x.

print(t.index(3))   # 3 → index 3

7. Advantages of Tuples

✔ Faster than lists
✔ Require less memory
✔ Used for fixed data
✔ Can be used as keys in dictionaries (because they are immutable)
✔ Data security — accidental modifications are prevented


8. Tuple Packing and Unpacking

Packing:

person = ("John", 25, "India")

Unpacking:

name, age, country = person
print(name)
print(age)
print(country)

9. Nested Tuples

Tuples inside tuples:

t = (1, 2, (3, 4), 5)
print(t[2][1])  # 4

10. Checking Membership

t = (10, 20, 30)
print(20 in t)        # True
print(40 not in t)    # True

11. Joining Tuples

Using +:

t1 = (1, 2)
t2 = (3, 4)
t3 = t1 + t2

Repeating tuple:

t = (1, 2) * 3
print(t)     # (1, 2, 1, 2, 1, 2)

12. Iterating Through Tuples

t = ("a", "b", "c")
for x in t:
    print(x)

13. Length of Tuple

len((1, 2, 3))   # 3

14. Examples for Practice

Example 1: Maximum and Minimum

t = (10, 50, 30)
print(max(t))
print(min(t))

Example 2: Tuple of even numbers

even = tuple(i for i in range(1, 11) if i % 2 == 0)
print(even)

Summary Table: Tuples

PropertyDetail
TypeSequence
Mutable❌ No (immutable)
Ordered✔ Yes
Allow duplicates✔ Yes
Allow different data types✔ Yes
Syntax( ) or comma-separated values
Methodscount(), index()