Tuples | Dataplexa

Tuples in Python

Tuples are another important data structure in Python, similar to lists but with one major difference: Tuples are immutable — meaning their values cannot be changed once created. This feature makes tuples reliable for storing data that should not be modified, such as configuration values, coordinates, fixed sets of items, and database records.

Tuples are widely used in data science, backend APIs, and large-scale applications where protecting data from accidental modification is important.

What Is a Tuple?

A tuple is an ordered collection of items enclosed in ( ). Like lists, tuples can store multiple data types — numbers, strings, booleans, or even other tuples.

my_tuple = (10, "Dataplexa", 3.14, True)

Because tuples are immutable, they are safer and sometimes faster than lists.

Why Use Tuples?

  • They are faster than lists (performance advantage)
  • They cannot be changed accidentally (safer storage)
  • They can be used as dictionary keys (lists cannot)
  • Useful for fixed data like coordinates, RGB values, settings
  • Memory-efficient compared to lists

Creating Tuples

You can create tuples in several ways:

t1 = (1, 2, 3)
t2 = ("python", "java")
t3 = (10, "hello", True)
t4 = ()            # empty tuple
t5 = (5,)          # single element tuple (comma required)

A tuple with one element must include a comma, otherwise Python treats it as a normal value.

Accessing Tuple Elements (Indexing)

Indexing works exactly like lists.

colors = ("red", "green", "blue")

print(colors[0])     # red
print(colors[2])     # blue
print(colors[-1])    # blue (last element)

Slicing Tuples

You can extract parts of a tuple using slicing syntax.

nums = (10, 20, 30, 40, 50)

print(nums[1:4])    # (20, 30, 40)
print(nums[:3])     # (10, 20, 30)
print(nums[2:])     # (30, 40, 50)

Tuple Immutability

Once a tuple is created, its values cannot be changed. If you try to modify a tuple, Python throws an error.

t = (1, 2, 3)
t[1] = 10   # ❌ Error: tuples are immutable

To "modify" a tuple, you must create a new tuple.

Tuple Methods

MethodDescription
count(x)Returns number of times x appears
index(x)Returns index of first occurrence of x

Example of Tuple Methods

nums = (10, 20, 30, 20, 40)

print(nums.count(20))   # 2
print(nums.index(30))   # 2

Iterating Through a Tuple

Loops allow you to access each element in a tuple.

fruits = ("apple", "banana", "mango")

for f in fruits:
    print(f)

Tuple Packing and Unpacking

Python allows assigning multiple values to multiple variables using tuples.

# Packing
data = ("Sreekanth", 25, "USA")

# Unpacking
name, age, country = data

print(name)
print(age)
print(country)

Nesting Tuples

You can store one tuple inside another — useful for representing structured data.

student = ("John", (85, 90, 95))

print(student[0])        # John
print(student[1][1])     # 90

Real-World Example: GPS Coordinates

Coordinates must not change, making tuples a perfect choice.

location = (37.7749, -122.4194)   # San Francisco
print("Latitude:", location[0])
print("Longitude:", location[1])

Real-World Example: Database Record

Tuples are often used to represent a row fetched from a database.

user = ("Sreekanth", "sreekanth@example.com", "Active")

print("Name:", user[0])
print("Email:", user[1])
print("Status:", user[2])

📝 Practice Exercises


  1. Create a tuple of 5 cities and print the second and fourth city.
  2. Write a program to count how many times a number appears in a tuple.
  3. Unpack a tuple containing your name, age, and country.
  4. Create a nested tuple representing a student's name and three marks.
  5. Write a program to check if a given value exists in a tuple.

✅ Practice Answers


Answer 1:

cities = ("Delhi", "Mumbai", "Chennai", "Kolkata", "Hyderabad")
print(cities[1], cities[3])

Answer 2:

nums = (1,2,3,2,4,2)
print(nums.count(2))

Answer 3:

data = ("Sreekanth", 25, "USA")
name, age, country = data
print(name, age, country)

Answer 4:

student = ("Ravi", (87, 90, 85))
print(student)

Answer 5:

nums = (5, 10, 15, 20)
value = 15

if value in nums:
    print("Found")
else:
    print("Not Found")