PYTHON • Beginner

Python Tuples

A tuple is an ordered collection of items that cannot be changed after it is created. Tuples look like lists, but they are immutable — which makes them safer for fixed data, slightly faster, and usable as dictionary keys.

Creating a Tuple

# With round brackets
marks = (78, 85, 92)
student = ("Ankit", 21, "Varanasi")

# Brackets are optional (tuple packing)
point = 10, 20

# A single-item tuple needs a comma!
one = (5,)      # tuple
not_tuple = (5) # just the number 5

# Empty tuple and tuple() from a list
empty = ()
t = tuple([1, 2, 3])
⚠️ Common Mistake

(5) is not a tuple — it is just the integer 5 in brackets. Always add a comma for a single-item tuple: (5,).

Accessing Items: Indexing and Slicing

Tuples are ordered, so indexing and slicing work exactly like lists:

days = ("Mon", "Tue", "Wed", "Thu", "Fri")

print(days[0])     # Mon
print(days[-1])    # Fri
print(days[1:4])   # ('Tue', 'Wed', 'Thu')
print(len(days))   # 5
print("Wed" in days)  # True

Tuples Are Immutable

You cannot add, remove or change items in a tuple:

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

# To "change" a tuple, create a new one
colors = ("yellow",) + colors[1:]
print(colors)   # ('yellow', 'green', 'blue')

Packing and Unpacking

Unpacking assigns each item of a tuple to a separate variable — one of Python's most useful features:

student = ("Priya", 20, "BCA")
name, age, course = student
print(name, course)    # Priya BCA

# Swap two variables without a temp variable
a, b = 5, 9
a, b = b, a
print(a, b)   # 9 5

# Use * to collect the rest
first, *rest = (1, 2, 3, 4)
print(first, rest)   # 1 [2, 3, 4]

Tuple Methods

Because tuples cannot change, they have only two methods:

nums = (2, 4, 2, 8, 2)
print(nums.count(2))   # 3  → how many times 2 appears
print(nums.index(8))   # 3  → position of the first 8

Returning Multiple Values from a Function

def min_max(numbers):
    return min(numbers), max(numbers)   # returns a tuple

low, high = min_max([45, 12, 89, 33])
print(low, high)   # 12 89

Tuple vs List — When to Use Which?

ListTuple
Syntax[1, 2, 3](1, 2, 3)
Can change?Yes (mutable)No (immutable)
MethodsMany (append, remove, sort…)Only count() and index()
Dictionary key?NoYes (if items are immutable)
Use forData that grows or changesFixed data: coordinates, dates, records
💡 Academy Tip

If the data should never change — like the days of the week or a (latitude, longitude) pair — use a tuple. It protects you from changing it by mistake.

📝 Try Yourself

Create a tuple of 5 subject marks. Print the highest mark, the lowest mark and the average. Then unpack the first two marks into variables m1 and m2 and swap them.

Quick Revision

  • Tuples are ordered and immutable.
  • Created with () or just commas; single item needs (x,).
  • Indexing, slicing, len() and in work like lists.
  • Only two methods: count() and index().
  • Unpacking: a, b = (1, 2); swapping: a, b = b, a.
  • Tuples can be dictionary keys; lists cannot.

Interview Questions

Q: What is the difference between a list and a tuple?

A: Lists are mutable (items can be changed), tuples are immutable. Tuples use slightly less memory, can be dictionary keys, and are used for fixed data.

Q: How do you create a tuple with one element?

A: Add a trailing comma: t = (5,). Without the comma, (5) is just an integer.

Q: Can a tuple contain a list? Can that list change?

A: Yes. The tuple itself cannot be reassigned item-by-item, but a mutable object inside it (like a list) can still be modified: t = ([1, 2], 3); t[0].append(9) works.

Q: What is tuple unpacking?

A: Assigning each element of a tuple to a variable in one line, e.g. name, age = ("Ravi", 22).

Want to Master Python?

Join our Python course in Varanasi — practical classes and career support at our Sarnath centre.

Ask on WhatsApp
Chat with us