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