Python Tuples
A Tuple is a collection data type in Python used to store multiple values in a single variable. Tuples are ordered and immutable.
Why Tuples are Important?
- Tuples store multiple values together.
- Tuples are faster than lists.
- Tuples protect data because they are immutable.
- Tuples are useful for fixed data.
- Tuples support indexing and slicing.
Creating a Tuple
Tuples are created using parentheses ().
numbers = (10, 20, 30, 40) print(numbers)
Output
(10, 20, 30, 40)
Here:
- numbers is the tuple name.
- 10, 20, 30, and 40 are tuple elements.
- Elements are separated using commas.
Tuple with One Element
To create a tuple with one element, comma is required.
data = (10,) print(type(data))
Output
<class 'tuple'>
Without comma, Python treats it as integer instead of tuple.
Tuple can Store Different Data Types
data = (10, "Python", 99.5, True) print(data)
Output
(10, 'Python', 99.5, True)
Tuple Indexing
Each tuple element has an index number. Indexing starts from 0.
fruits = ("Apple", "Mango", "Orange")
print(fruits[0])
print(fruits[1])
print(fruits[2])
Output
Apple Mango Orange
Negative Indexing
Negative indexing starts from the end.
fruits = ("Apple", "Mango", "Orange")
print(fruits[-1])
print(fruits[-2])
Output
Orange Mango
Tuple Slicing
Slicing returns multiple tuple elements.
numbers = (10, 20, 30, 40, 50) print(numbers[1:4])
Output
(20, 30, 40)
Python starts from index 1 and stops before index 4.
Tuples are Immutable
Tuple values cannot be changed after creation.
fruits = ("Apple", "Mango", "Orange")
fruits[1] = "Banana"
Output
TypeError
Python gives error because tuples do not allow modification.
Looping Through Tuple
Tuples work with loops just like lists.
colors = ("Red", "Blue", "Green")
for item in colors:
print(item)
Output
Red Blue Green
Finding Tuple Length
Use len() function to count elements.
numbers = (10, 20, 30, 40) print(len(numbers))
Output
4
count() Method
count() returns how many times an element appears.
numbers = (10, 20, 10, 30, 10) print(numbers.count(10))
Output
3
index() Method
index() returns the position of an element.
fruits = ("Apple", "Mango", "Orange")
print(fruits.index("Mango"))
Output
1
Tuple Packing
Packing means storing multiple values into one tuple.
data = ("Ali", 20, 85)
Python automatically packs values into tuple.
Tuple Unpacking
Unpacking extracts tuple values into variables.
data = ("Ali", 20, 85)
name, age, marks = data
print(name)
print(age)
print(marks)
Output
Ali 20 85
Joining Tuples
Tuples can be joined using + operator.
tuple1 = (1, 2) tuple2 = (3, 4) result = tuple1 + tuple2 print(result)
Output
(1, 2, 3, 4)
Repeating Tuples
numbers = (10, 20) print(numbers * 3)
Output
(10, 20, 10, 20, 10, 20)
Checking if Item Exists
Use in keyword to check elements.
fruits = ("Apple", "Mango", "Orange")
print("Mango" in fruits)
print("Banana" in fruits)
Output
True False
Nested Tuples
Tuple inside another tuple is called nested tuple.
data = (
(1, 2),
(3, 4)
)
print(data)
Output
((1, 2), (3, 4))
Difference Between List and Tuple
- Lists use square brackets [].
- Tuples use parentheses ().
- Lists are mutable.
- Tuples are immutable.
- Tuples are faster than lists.
Real Life Example
Tuples are useful for storing fixed student data.
student = ("Ali", 20, "Delhi")
print(student[0])
print(student[1])
print(student[2])
Output
Ali 20 Delhi
Summary
- Tuples store multiple values.
- Tuples are immutable.
- Tuples support indexing and slicing.
- Tuples are faster than lists.
- count() counts elements.
- index() finds element position.
- Tuples support packing and unpacking.