Welcome, curious coder! Today, we’ll be learning about one of Python’s important data structures called Tuples. If you’ve already learned about lists, then learning about tuples will feel like adding another magical tool to your coding toolkit.


🧺 What Is a Tuple?

Imagine you have a small basket where you drop in two fruits — say, an apple and a banana. Once you tie the basket shut, you’re not allowed to open it again or change what’s inside.

That’s what a tuple is in Python — a group of items that you can’t change once you create it. This special group is ordered, so you can still check what’s inside and where everything is — but you can’t add, remove, or swap the items.

Let’s see an example:

names = ("Roger", "Syd")

That’s a tuple with two names inside it — Roger and Syd. Notice how we used parentheses () to define it. This is different from a list which uses square brackets [].


📦 Tuples Are Ordered

Just like items in a queue, the first one in is at position 0, the next at position 1, and so on.

print(names[0])  # Output: Roger
print(names[1])  # Output: Syd

Want to check the last item quickly? Use a negative index:

print(names[-1])  # Output: Syd

Yes, -1 gives us the last item, -2 gives the second last, and so on.


🔍 Finding Items with index()

You can find where an item is using the index() method:

names.index("Roger")  # Output: 0

⚠️ Important: If the item isn’t in the tuple, Python will show an error. So be careful!


📏 How Many Items?

To count how many things are in your tuple, use len():

print(len(names))  # Output: 2

🔎 Is It There?

To check if an item exists inside your tuple, use the in keyword:

print("Roger" in names)  # Output: True
print("Tina" in names)   # Output: False

✂️ Slicing Tuples

You can slice a tuple — that means cut out a part of it, just like slicing cake 🍰:

print(names[0:2])  # Output: ('Roger', 'Syd')
print(names[1:])   # Output: ('Syd',)

🎯 Sorted Tuples

Tuples can’t be changed, but you can create a sorted list from a tuple:

sorted_names = sorted(names)
print(sorted_names)  # Output: ['Roger', 'Syd']

Note that this returns a list, not a tuple. Why? Because tuples are unchangeable!


➕ Joining Tuples

You can add (concatenate) tuples together using the + symbol:

new_tuple = names + ("Vanille", "Tina")
print(new_tuple)
# Output: ('Roger', 'Syd', 'Vanille', 'Tina')

Just like magic, you’ve created a brand new tuple!


🎓 In Summary

Tuples are like locked containers:

  • ✅ You can look inside
  • ✅ You can count items
  • ✅ You can slice parts of them
  • ❌ But you can’t change, add, or remove items

Use them when you want to store values that shouldn’t be changed — like months of the year or the names of planets.


📝 Practice Questions

  1. Create a tuple called colors with the items "red", "blue", and "green". Print the first item.

  2. Check if the word "yellow" is in the colors tuple.

  3. Create a new tuple by joining colors with another tuple that has "orange" and "purple". Print the result.

  4. Use slicing to get the last two colors from the colors tuple.

  5. Use the len() function to find out how many items are in your colors tuple.


<
Previous Post
🍪💾 The Tale of Locky and Cookie: How the Web Remembers Stuff
>
Next Post
🗒️ A Tab, a Notebook, and a Memory: Understanding sessionStorage in JavaScript