Tuples in Python: Definition, Methods, and Code Examples
Master Python tuples with clear examples covering creation, indexing, slicing, immutability, and the 2 built-in methods tested in placement drives.
A tuple in Python is an ordered, immutable sequence: once defined, its elements cannot be reassigned, deleted, or reordered. The Python language reference classifies tuples as one of four built-in sequence types, alongside lists, ranges, and strings.
What Is a Tuple in Python?
Tuples hold multiple values in a single variable. Unlike a list, a tuple’s structure is fixed at creation time: attempting to modify an element raises a TypeError. That constraint sounds limiting, but it’s exactly what you want when data should remain constant for the life of a program.
Four key properties define a tuple:
- Ordered — elements stay in insertion order; index 0 is always the first item.
- Immutable — no item assignment, no
append(), noremove(). The tuple is sealed. - Heterogeneous — a single tuple can hold integers, strings, floats, and even other tuples.
- Hashable — because tuples are immutable, they can serve as dictionary keys and set members, provided all their elements are also hashable.
Typical use cases: coordinates like (lat, lon), RGB colour values like (255, 0, 128), and fixed configuration settings that a function reads but should never modify. When a function returns several related values, Python’s convention is to pack them into a tuple.
Creating Tuples: Four Patterns
Empty tuple
empty = ()
print(type(empty)) # <class 'tuple'>
Single-element tuple
The trailing comma is mandatory. Without it, Python reads parentheses as a grouping operator and returns the enclosed value as-is.
not_a_tuple = (42)
single = (42,)
print(type(not_a_tuple)) # <class 'int'>
print(type(single)) # <class 'tuple'>
This is one of the most common mistakes in placement coding rounds; the trailing comma is easy to miss and produces a confusing bug only when you try to iterate over the supposed “tuple.”
Multi-element tuple
Parentheses are optional when there is no syntactic ambiguity. Python infers a tuple from the comma-separated structure.
t1 = (10, 20, "Python") # with parentheses
t2 = 10, 20, "Python" # without parentheses — still a tuple
print(t1) # (10, 20, 'Python')
print(t2) # (10, 20, 'Python')
The tuple() constructor
Any iterable can be converted to a tuple with tuple().
a = tuple([38, 4.9, "hello"])
print(a) # (38, 4.9, 'hello')
Nested Tuples
A tuple can contain another tuple as one of its elements. Accessing a nested element requires chaining the index operators.
nested = (10, (20, 30))
print(nested) # (10, (20, 30))
t1 = (10, 20)
t2 = ("Python",)
t3 = (t1, t2)
print(t3) # ((10, 20), ('Python',))
Accessing and Slicing Tuple Elements
Python uses 0-based indexing. Negative indices count from the right: -1 is the last element, -2 is the second-to-last, and so on.
t = ("Python", 10, 20.3)
print(t[0]) # Python
print(t[1]) # 10
print(t[-1]) # 20.3
Slicing returns a new tuple containing the selected range. The stop index is excluded, matching Python’s standard slice convention.
t = (30, "Python", 30.34, 40, 20.24)
print(t[1:3]) # ('Python', 30.34)
print(t[1:]) # ('Python', 30.34, 40, 20.24)
For nested tuples, chain the index operators to reach inner elements.
nested = (("Welcome", 10, 20.3), (20, "Hello"))
print(nested[0][2]) # 20.3
print(nested[1][0]) # 20
Tuple Immutability: What Changes and What Does Not
Attempting to reassign a tuple element raises a TypeError immediately. No partial update is possible.
t = (30, "Python", 30.34, 40, 20.24)
t[2] = "Hello"
# TypeError: 'tuple' object does not support item assignment
Mutable elements inside a tuple
The immutability rule applies to the tuple’s own references, not to the objects those references point to. If a tuple holds a list, that list can be modified.
t = (30, "Python", [30.34, 40, 20.24])
t[2][0] = "Hello"
print(t) # (30, 'Python', ['Hello', 40, 20.24])
The tuple still holds the same three references. Only the content of the third reference (the list) changed.
Variable rebinding is not mutation
Reassigning the variable name binds it to a new tuple. The original tuple is unchanged; Python’s garbage collector cleans it up when nothing else references it.
t = (30, "Python", 30.34, 40, 20.24)
t = (20, 1.45, "hello")
print(t) # (20, 1.45, 'hello')
Understanding this distinction is a common interview sub-question: “is t the same tuple after reassignment?” The answer is no.
Built-in Tuple Methods and Operations
Python provides exactly 2 built-in methods for tuples: count() and index(). Lists have 11. The difference reflects the read-only design: you can query a tuple’s contents; you cannot reshape them.
count() and index()
t = (30, "Python", 30.34, 30)
print(t.count(30)) # 2 — 30 appears at index 0 and index 3
print(t.index("Python")) # 1 — "Python" first appears at index 1
index() raises a ValueError if the value is not found, so wrap it in a try-except in production code.
Concatenation and repetition
The + operator creates a new tuple by joining two tuples. The * operator repeats elements.
t1 = (30, "Python", 30.34)
t2 = (20, 1.45, "hello")
print(t1 + t2) # (30, 'Python', 30.34, 20, 1.45, 'hello')
print(t1 * 2) # (30, 'Python', 30.34, 30, 'Python', 30.34)
Neither + nor * modifies either original tuple. Both return new tuple objects.
Membership test
t = (30, "Python", 30.34)
print(30 in t) # True
print("faceprep" in t) # False
Tuple packing and unpacking
Python lets you unpack a tuple’s elements directly into named variables. The number of variables must match the number of elements, or a ValueError is raised.
coordinates = (12.97, 77.59)
lat, lon = coordinates
print(lat) # 12.97
print(lon) # 77.59
Unpacking is how multiple-return-value functions work in practice, and it appears in placement test questions more often than students expect.
For more practice iterating over sequences, see sum of array elements in Python and sort a string alphabetically in Python.
Tuples in Python Placement Drives
Python coding rounds appear in on-campus placement tests at Tier-2 and Tier-3 engineering colleges across CSE, IT, and ECE branches. Tuple questions follow consistent patterns. Recognising those patterns before a test is worth the time.
Common question types and what each one tests:
| Pattern | What it tests |
|---|---|
”What is the output?” given t = (5,) vs t = (5) | Trailing comma rule for single-element tuples |
Attempt t[0] = 99 on an existing tuple | TypeError on immutable element assignment |
| Modify a list inside a tuple | Mutable inner elements are a valid exception |
Call t.count(x) and t.index(x) | Recall of the only 2 built-in tuple methods |
| Use a tuple as a dictionary key | Hashability versus list unhashability |
Unpack a, b = (1, 2, 3) | ValueError on count mismatch in unpacking |
The Python tutorial on data structures covers the formal specification behind each of these behaviours, which is the safest cross-reference when you want to confirm an answer rather than rely on a practice platform’s answer key.
For a broader set of Python practice problems that complement tuple study, see Python example programs for practice.
The immutability discipline tuples enforce, knowing which data to freeze and which to leave mutable, carries directly into building LLM application code. When you call a model API, the request parameters (model name, temperature, token limit) should not change mid-run: a tuple communicates that constraint to every reader of your code in a way a list does not. TinkerLLM is where you apply that Python thinking to real model calls, starting at ₹299.
Primary sources
Frequently asked questions
What is the difference between a tuple and a list in Python?
Tuples are immutable (elements cannot be changed after creation) while lists are mutable. Tuples use parentheses; lists use square brackets. Because tuples are immutable they are also hashable, so they can serve as dictionary keys.
How do you create a tuple with a single element in Python?
Add a trailing comma: my_tuple = (42,). Without the comma, Python treats (42) as the integer 42 in parentheses, not a tuple. The trailing comma is mandatory for any single-element tuple.
Can you modify elements inside a tuple?
Direct element assignment raises a TypeError. However, if a tuple contains a mutable element such as a list, that inner list can be modified: t = (1, [2, 3]) allows t[1][0] = 99 without error.
What are the built-in methods available for tuples in Python?
Python tuples have exactly 2 built-in methods: count(value) returns how many times a value appears in the tuple, and index(value) returns the position of the first occurrence of that value.
Can a tuple be used as a dictionary key in Python?
Yes. Because tuples are immutable and therefore hashable, they can serve as dictionary keys. Lists are mutable and unhashable, so they cannot be used as dictionary keys.
Why do placement tests ask about Python tuples?
Interviewers use tuples to test understanding of mutability versus immutability, a concept that extends to dictionary keys, function return values, and memory-efficient data representation in Python programs.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)