Skip to content
Subin Thapa

Subin Thapa

  • Home
  • About
  • Service
  • Portfolio
  • Blog
  • Contact
Schedule Meeting

Day-24: Mastering Tuples in Python – Immutable Data Structures

subinthapaSeptember 19, 2025October 6, 2025 No Comments

Today on Day-24 of our coding challenge, we are diving into Tuples, one of Python’s most useful data structures.

A tuple is like a list, but with one key difference — you cannot change it after creation. Tuples are perfect for storing fixed collections of data, such as days of the week, stock names, or coordinate values. Tuples are represented by parentheses ( ).


What is a Tuple?

coordinate = (10.0, 20,"Subin Thapa","RamHari Thapa")

Here, we created a tuple named coordinate. It contains 4 items, representing A, B, C, and D coordinates.


Tuple Features ✨

  • Ordered: Items maintain the order you put them in.
  • Immutable: Once created, you cannot change a tuple.
  • Allows Duplicates: Tuples can hold the same value multiple times, e.g., (1, 2, 2, 3).
  • Mixed Items: You can store numbers, strings, and other data types together.

Important Methods Used in Tuples

Method / OperationDescription
count(value)Counts how many times value appears
index(value)Returns index of first occurrence
len(tuple)Returns the number of items
value in tupleChecks if value exists
tuple[i]Indexing
tuple[start:end:step]Slicing
min(tuple)Returns minimum value
max(tuple)Returns maximum value
sum(tuple)Calculates the sum of numbers
tuple inside listA tuple can contain a list, which can be updated even though the tuple itself is immutable

Methods That Don’t Work ❌

Because tuples are immutable, these methods cannot be used directly:

  • append()
  • extend()
  • insert()
  • remove()
  • pop()
  • reverse()
  • clear()

To use these methods, first convert the tuple into a list.

Example:

tuple_data = ("Subin Thapa", "Garima Sapkota","Bishworaj Poudel","Gokarna Parajuli")
result = list(tuple_data)  # Converting tuple into list
print(result)

Using Tuples in Everyday Life

Create a Tuple

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

Access Tuple Items

first_color = colors[0]  # "red"

Immutable Nature

colors[0] = "yellow"  # Error

Tuple with Mixed Items

person = ("Alice", 30, "Engineer")

Example 1: Storing Days of the Week

days_of_week = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
first_day = days_of_week[0]
last_day = days_of_week[-1]
print("First Day of the Week:", first_day)
print("Last Day of the Week:", last_day)

Example 2: Combining Tuples

odd_numbers = (1, 3, 5)
even_numbers = (2, 4, 6)
all_numbers = odd_numbers + even_numbers
print(f"Combined Tuple: {all_numbers}")

Example 3: Tuple Length

fruit_tuple = ("apple", "banana", "cherry")
num_fruits = len(fruit_tuple)
print(f"Number of Fruits: {num_fruits}")

Example 4.1: Basic Slicing

numbers = (10, 20, 30, 40, 50)
sliced = numbers[1:4]  # from index 1 to 3
print(sliced)

Example 4.2: Skipping Elements

numbers = (10, 20, 30, 40, 50)
sliced = numbers[0:5:2]  # step = 2
print(sliced)

Example 4.3: Negative Indices

numbers = (10, 20, 30, 40, 50)
sliced = numbers[-4:-1]  # from index -4 to -2
print(sliced)

Example 5: Nested Tuples

nested = (("apple", "banana"), (1, 2, 3))
print(nested[0][1])  # banana
print(nested[1][2])  # 3

Example 6: Tuple Packing & Unpacking

# Packing
person = ("Subin", 18, "Student")

# Unpacking
name, age, role = person

print(name)  # Subin
print(age)   # 18
print(role)  # Student

Challenge 24 🏆

  1. Create a tuple of your favorite bank stocks and display all of them.
  2. Access and print the first and last bank stock in the tuple.
  3. Attempt to change the second bank stock in the tuple (should raise an error).
  4. Combine this tuple with another tuple of your favorite hydro stocks, and print the resulting combined tuple.
  5. Find and print the length of the combined tuple using the len() function.

👉 For more tutorials like this, visit my website and don’t forget to subscribe to my YouTube channel: Coding with Subin 🚀

Post navigation

Previous: Day 60 Challenge: How to Improve Typing Speed and Accuracy
Next: Why Smart Investors Use Both Fundamental & Technical Analysis Together

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2025 Subin Thapa
No Form Selected This form is powered by: Sticky Floating Forms Lite