Ordered like a list, locked like a string. The single-element comma gotcha, tuple vs list, and why immutability protects data integrity.
A tuple is a fundamental Python data structure used to store multiple items in a single variable. It is an ordered, immutable (read-only) collection of items, created using round brackets () with items separated by commas. Tuples are similar to lists, but once created, their contents cannot be changed (no editing, no adding, no deleting individual items). Tuples can hold mixed data types and can be nested (a tuple inside a tuple).
len, min, max, sum, sorted) work on tuples.del, but individual elements cannot.>>> T1 = ()
>>> T1
()
>>> T2 = (1, 2, 3, 4, 5)
>>> T2
(1, 2, 3, 4, 5)
>>> T3 = ("Hello", 4, 5.6)
>>> T3
('Hello', 4, 5.6)
>>> T4 = (1, 2, 3, (4, 5)) # nested tuple
>>> T4
(1, 2, 3, (4, 5))
Creating a tuple using the tuple() constructor:
>>> T6 = tuple("Goa")
>>> T6
('G', 'o', 'a') # string gets split into individual characters
>>> T6 = tuple([1, 2, 3, 4, 5])
>>> T6
(1, 2, 3, 4, 5) # list converted into a tuple
Putting a single value inside parentheses without a trailing comma does not create a tuple โ Python just evaluates it as that value's own type (int, str, etc.). To define a single-element tuple, you must include a trailing comma , after the element.
>>> T5 = (1)
>>> type(T5)
<class 'int'> # NOT a tuple! Just an integer in parentheses
>>> T5 = ("Hello")
>>> type(T5)
<class 'str'> # NOT a tuple! Just a string in parentheses
>>> T5 = ("Hello",) # trailing comma makes it a tuple
>>> T5
('Hello',)
>>> type(T5)
<class 'tuple'>
Same idea with a plain int/single-element case:
# Incorrect way (without trailing comma)
not_a_tuple = (5)
print(type(not_a_tuple)) # Output: <class 'int'>
# Correct way (with trailing comma)
single_element_tuple = (5,)
print(type(single_element_tuple)) # Output: <class 'tuple'>
Rule to remember: A single value in()is not a tuple unless you add a trailing comma,after it โ(item,).
Tuples support element retrieval using indexing and slicing, identical to lists โ zero-based indexing, negative indexing, and slicing.
A. Positive Indexing โ starts at 0 for the first element.
numbers = (10, 20, 30, 40, 50)
print(numbers[0]) # 10
print(numbers[2]) # 30
B. Negative Indexing โ counts backward from the end; -1 is the last element.
>>> T2 = (1, 2, 3, 4, 5)
>>> T2[-1]
5
C. Slicing โ tuple[start:end] extracts a sub-range (end index excluded).
>>> T2[:4]
(1, 2, 3, 4)
numbers = (10, 20, 30, 40, 50)
print(numbers[1:4]) # (20, 30, 40) -> starts at index 1, excludes index 4
Accessing nested tuple elements (chain index brackets):
>>> T4 = (1, 2, 3, (4, 5))
>>> T4[-1][0]
4 # T4[-1] -> (4, 5), then [0] -> 4
Immutability means an object's state or contents cannot be modified after creation. Tuples are immutable, behaving similarly to strings.
>>> L = [1, 2, 3, 4, 5]
>>> L[0] = 100
>>> L
[100, 2, 3, 4, 5] # โ
works fine for lists
>>> T2 = (1, 2, 3, 4, 5)
>>> T2[0] = 100
Traceback (most recent call last):
...
TypeError: 'tuple' object does not support item assignment
Tuples do not support adding new items after creation โ there's no append, insert, etc., because of immutability.
del.>>> T1
()
>>> del T1
>>> T1
Traceback (most recent call last):
...
NameError: name 'T1' is not defined
>>> T2 = (1, 2, 3, 4, 5)
>>> del T2[-1]
Traceback (most recent call last):
...
TypeError: 'tuple' object doesn't support item deletion
| Feature | Tuple (tuple) | List (list) |
|---|---|---|
| Syntax | Round brackets () | Square brackets [] |
| Mutability | Immutable (cannot be changed) | Mutable (can be changed) |
| Editing Elements | Not allowed (raises TypeError) | Allowed (list[i] = new_value) |
| Adding New Items | Not allowed | Allowed (e.g. append()) |
| Access Category | Read-only data structure | Read & write data structure |
๐ก Key Revision Point: The primary difference between a Tuple and a List is that Tuples are immutable, whereas Lists are mutable.
Concatenation (+) โ joins two tuples into a new one:
>>> T2 = (1, 2, 3, 4, 5)
>>> T3 = ("Hello", 4, 5.6)
>>> T2 + T3
(1, 2, 3, 4, 5, 'Hello', 4, 5.6)
Repetition (*) โ repeats the tuple's contents:
>>> T2 * 3
(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
Iteration (for loop):
>>> for i in T2:
print(i)
1
2
3
4
5
Membership test (in):
>>> 1 in T2
True
| Function | Example | Result | Purpose |
|---|---|---|---|
len() | len(T2) | 5 | Number of items |
min() | min(T2) | 1 | Smallest item |
max() | max(T2) | 5 | Largest item |
sum() | sum(T2) | 15 | Sum of all items (numeric tuples) |
sorted() | sorted(T2) | [1, 2, 3, 4, 5] | Returns a sorted list (ascending) โ โ ๏ธ output is a list, not a tuple! |
sorted(reverse=True) | sorted(T2, reverse=True) | [5, 4, 3, 2, 1] | Sorted in descending order |
>>> T2 = (1, 2, 3, 4, 5)
>>> len(T2)
5
>>> min(T2)
1
>>> max(T2)
5
>>> sum(T2)
15
>>> sorted(T2)
[1, 2, 3, 4, 5]
>>> sorted(T2, reverse=True)
[5, 4, 3, 2, 1]
sorted() sorts the tuple values and returns a new list object, keeping the original tuple intact.
Think of tuple operations in terms of Read vs. Write permissions:
Because write operations are blocked, tuples serve as a read-only data structure.
Impact of data-type choice:
list[index] = 8.5), destroying data integrity.๐ก Key takeaway: Use tuples whenever data integrity is critical and information must remain tamper-proof throughout the application's lifecycle.
().(x,) โ without it, it's just x.del T).TypeError: 'tuple' object does not support item assignment.+ (concat), * (repeat), for loop, in (membership).len, min, max, sum, sorted (returns a list, not a tuple).tuple(iterable) to build a tuple from a string or list.