A tuple is a built-in Python data structure used to store multiple values
inside a single variable.
Python provides four main collection data types:
List
Tuple
Set
Dictionary
Each collection type has unique behavior and use cases.
What Is a Tuple?
A tuple is:
Ordered – items maintain their position
Immutable – values cannot be changed after creation
Allows duplicates
Tuples are created using round brackets
().
Creating a Tuple
Tuple Characteristics
Ordered
Items inside a tuple have a fixed order that never changes.
Immutable
Once a tuple is created, its elements cannot be modified.
Allows Duplicate Values
Tuples can store repeated elements.
Tuple Length
To find how many elements a tuple contains, use the
len()
function.
Creating a Tuple with One Item
A single-item tuple must include a trailing comma, otherwise Python treats
it as a standard string.
Tuple Items and Data Types
Tuple elements can be of any data type.
Checking Tuple Data Type
Using the tuple() Constructor
Note: Notice the use of double brackets when converting
another collection into a tuple.
Accessing Tuple Elements in Python
You can retrieve values from a tuple by referring to their index position
using square brackets
[].
Access by Index
Negative Indexing
Accessing a Range of Items
You can extract multiple items by specifying a start index and an end index.
Slice from the Beginning
Slice Until the End
Using Negative Index Ranges
Checking if an Item Exists
Updating Tuples in Python
Tuples in Python are immutable, which means once they are created, you
cannot directly modify, add, or remove their elements. However, Python
provides safe workarounds to achieve these operations.
Modifying Tuple Elements (Workaround)
Convert the tuple into a list, make the required change, and convert it back
into a tuple.
Adding New Items to a Tuple
Removing Items from a Tuple
Deleting a Tuple Entirely
Unpacking Tuples in Python
When you assign multiple values to a tuple, the process is called tuple
packing. Python also allows you to extract those values into separate
variables, which is known as tuple unpacking.
Basic Tuple Unpacking
Using the Asterisk (*) Operator
When the tuple contains more values than variables, use the
* operator to
collect extra values into a list.
Asterisk in the Middle
Looping Through Tuples in Python
Tuples are iterable objects, which means you can loop through their elements
using different looping techniques in Python.
Iterating Directly Over Tuple Values
Looping Using Index Positions
Iterating with a While Loop
Choosing the Right Loop
Loop Type
Best Use Case
for
loop
Simple iteration over values
for
with index
When index is required
while
loop
Manual control over iteration
Joining Tuples in Python
Python allows you to combine and repeat tuples using simple operators. These
operations always create new tuples, because tuples are immutable.
Combining Multiple Tuples
Joining More Than Two Tuples
Repeating Tuple Elements
The
* operator
allows you to repeat the elements of a tuple a specified number of times.
Tuple Methods in Python
Tuples in Python are immutable, which means they have very few built-in
methods. Unlike lists, tuples support only two main methods for working with
their data.
Method
Purpose
count()
Counts occurrences of a value
index()
Returns the index of the first match
count() Method
index() Method
Handling Errors Safely
Before using
index(), it’s
a good practice to check whether the value exists.