All Courses
Python Basics

Python Tuples

Pronunciation: "tuple" (not "toople") — though both are commonly heard.


1. What is a Tuple?

A tuple is a collection of elements inside parentheses, separated by commas.

x = (1, 2, 3)
  • Works similarly to lists and strings
  • Elements are accessed by index
  • Immutable — elements cannot be modified after creation

2. Indexing

x = (1, 2, 3)
x[0]   # → 1  (first element)
x[1]   # → 2
x[-1]  # → 3  (last element, negative indexing works)

❌ Cannot Modify Elements

x[0] = 2   # → TypeError: tuple does not support item assignment

3. Immutability

Tuples are immutable — they cannot be changed in place.

Type Mutable?
list ✅ Yes
tuple ❌ No
str ❌ No
int ❌ No
float ❌ No
bool ❌ No

Workaround — Create a New Tuple to "Modify"

x = (1, 2, 3)
# Change the middle element to 4:
y = (x[0], 4, x[2])   # → (1, 4, 3)

4. Useful Operations

Length

len(x)   # → 3

.count()

Count how many times an element appears.

x.count(1)   # → 1

.index()

Returns the index of the first occurrence. Raises an error if not found.

x.index(1)   # → 0
x.index(9)   # → ValueError

Guide: Tuples use .index() (like lists), not .find() (which is strings-only).

in Operator

Check if an element exists in the tuple.

2 in x    # → True
4 in x    # → False

5. Nested Tuples

Tuples can contain other tuples, lists, booleans — any type.

x = (1, 2, True, [3, 4], (5, 6))
x[4]     # → (5, 6)
x[4][0]  # → 5

6. Combining & Multiplying

Adding Tuples

x = (1, 2)
y = (3, 4)
combined = x + y   # → (1, 2, 3, 4)

Multiplying Tuples

x * 2   # → (1, 2, 1, 2)

Common pattern — build a list/tuple pre-filled with the same value:

[1] * 10   # → [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
(0,) * 5   # → (0, 0, 0, 0, 0)

7. Creating Tuples

Standard (with parentheses)

x = (1, 2, 3)

Without parentheses

Comma-separated values on one line automatically form a tuple.

x = 1, 2, 3    # → (1, 2, 3)

⚠️ One-element tuple

A single value in parentheses is not a tuple — you need a trailing comma.

x = (1)    # → int, NOT a tuple
x = (1,)   # → (1,)  ✅ correct one-element tuple
x = 1,     # → (1,)  also valid

Using tuple()

tuple([1, 2, 3])   # → (1, 2, 3)

8. Tuple vs List

Feature List Tuple
Syntax [1, 2, 3] (1, 2, 3)
Mutable ✅ Yes ❌ No
Indexing ✅ Yes ✅ Yes
.count() ✅ Yes ✅ Yes
.index() ✅ Yes ✅ Yes
in operator ✅ Yes ✅ Yes
Addition / Multiplication ✅ Yes ✅ Yes

Use a tuple when you want data that should not change. Use a list when you need a modifiable collection.


Cheat Sheet

x = (1, 2, 3)

x[0]          # → 1
x[-1]         # → 3
len(x)        # → 3
x.count(1)    # → 1
x.index(2)    # → 1
2 in x        # → True
x + (4, 5)    # → (1, 2, 3, 4, 5)
x * 2         # → (1, 2, 3, 1, 2, 3)