What is a Tuple ?

Tuples are built-in datatype in python that are used to store multiple items in a single variable. A tuple is a collection which is ordered and unchangeable. A tuple is denoted by parentheses ( )


Features :

              Tuples are Ordered

              Tuples are Immutable - meaning once a tuple is created, the values cannot be changed. 

              Allow Duplicates

              Store Multiple data types


Each item in a tuple has a position called an indexIndexing starts from 0.

 
Python
numbers = (10, 20, 30, 40, 50)
Value1020304050
Forward Index01234
Negative Index-5-4-3-2-1

Accessing Tuple Items Using Indexing

Each element in a tuple has a position known as an index. Python uses zero-based indexing, so the first item has index 0.

1. Access the First Item

Python
nums = (5, 10, 15, 20) print(nums[0]) # Output: # 5

2. Access Any Item by Index

Python
fruits = ("apple", "banana", "cherry") print(fruits[2]) # Output: # cherry

Trying to access an index outside the range raises an IndexError.

Negative Indexing

Python supports negative indexing to access items from the end

Python
nums = (5, 10, 15, 20) print(nums[-1]) print(nums[-3]) # Output: # 20 # 10

Here:

  • -1 refers to the last item

  • -3 refers to the third-from-last item

Slicing Tuples

You can access a sub-tuple using slicing. The syntax is:

tuple[start:end]

This extracts items from index start up to (not including) index end.

1. Slice From Start to a Position

Python
letters = ("a", "b", "c", "d", "e") print(letters[0:3]) # Output: # ('a', 'b', 'c')

2. Slice From a Position to End

Python
letters = ("a", "b", "c", "d", "e") print(letters[2:]) # Output: # ('c', 'd', 'e')

3. Full Slice (Copy Entire Tuple)

Python
letters = ("a", "b", "c", "d", "e") print(letters[:]) # Output: # ('a', 'b', 'c', 'd', 'e')

Iterating Through a Tuple

You can use a for loop to access each item in a tuple

Python
fruits = ("apple", "banana", "cherry") for item in fruits: print(item) # Output: # apple # banana # cherry

Tuple Unpacking

You can extract values into separate variables

Python
person = ("Alice", 25, "Delhi") name, age, city = person print(name) print(age) print(city) # Output: # Alice # 25 # Delhi

This assigns each element to its corresponding variable.

Important Notes

  • Tuples are immutable — you cannot change items after creation.

  • Indexing and slicing work the same way as in lists.

  • Negative indexing lets you access items from the end.