Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Python Notes
Topic #8

Tuple in Python

What Is a Tuple?

A tuple is an ordered collection, just like a list — but immutable, meaning once created, it cannot be changed. Tuples are written with parentheses () instead of square brackets.

point = (28.70, 77.13)
colors = ("red", "green", "blue")

Creating a Tuple

single = (5,)          # note the comma — required for a single-item tuple
empty  = ()
mixed  = (1, "two", 3.0)
no_parens = 1, 2, 3     # parentheses are optional

Accessing Tuple Items

colors = ("red", "green", "blue")
print(colors[0])    # red
print(colors[-1])   # blue
print(colors[0:2])  # ('red', 'green')

Why Tuples Are Immutable

colors = ("red", "green", "blue")
colors[0] = "yellow"   # ❌ TypeError: 'tuple' object does not support item assignment

Once created, you cannot add, remove, or change items in a tuple. If you need a "modified" version, you create a new tuple.

Tuple Methods

numbers = (1, 2, 3, 2, 4, 2)
print(numbers.count(2))   # 3 — how many times 2 appears
print(numbers.index(3))   # 2 — index of first occurrence of 3

Tuple Unpacking

point = (28.70, 77.13)
lat, lng = point
print(lat)   # 28.70
print(lng)   # 77.13

a, b, *rest = (1, 2, 3, 4, 5)
print(rest)   # [3, 4, 5]

Why Use a Tuple Instead of a List?

  • Data integrity — use a tuple when values should never change, like coordinates or RGB colors
  • Performance — tuples are slightly faster than lists since Python doesn't need to manage resizing
  • Dictionary keys — tuples are hashable and can be used as dictionary keys; lists cannot
locations = {
    (28.70, 77.13): "Delhi",
    (19.07, 72.87): "Mumbai"
}

Key Takeaways

  • Tuples are ordered but immutable — you cannot modify them after creation
  • Use a trailing comma to create a single-item tuple: (5,)
  • Tuples are commonly used for fixed data and as dictionary keys

Want to go beyond the notes?

Join CodingNow's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →