Skip to main content

Command Palette

Search for a command to run...

Learning Tuples & Sets in Python: Concepts + Mini Project

Published
•3 min read•View as Markdown

Hey there, Pythonistas! 👋

Today I learned all about tuples and sets in Python. They’re both useful data types with their own unique properties—and to really understand them better, I wrapped things up with a fun mini-project!

Let me walk you through what I learned and built. 🛠️


📦 Tuples in Python

A tuple is an unordered and immutable collection of items. That means:

  • You can’t change a tuple after it’s created.

  • You use parentheses ( ), not square brackets [ ].

  • They are faster than lists and great for storing fixed data like coordinates, IDs, etc.

  • Tuples can be used as keys in dictionaries (unlike lists, which are mutable).

👇 Example:

my_tuple = (1, 2, 3)

print(my_tuple[0])     # Accessing elements
print(my_tuple[1:])    # Slicing
print(type(my_tuple))  # Checking type

# Tuple unpacking
x, y, z = my_tuple
print(x, y, z)

# One-item tuple needs a trailing comma
one_item = ("name",)

🌀 Sets in Python

A set is an unordered collection of unique elements.

  • You define sets using curly braces {} or the set() constructor.

  • Sets automatically remove duplicates.

  • Perfect for membership checking (e.g., if x in my_set)

  • Supports set theory operations like union and intersection.

👇 Example:

my_set = {1, 2, 3, 5}
my_set.add(4)       # Add an element
my_set.remove(2)    # Remove an element
print(my_set)
print(type(my_set))

# Constructor method
your_set = set([1, 2, 5])
print(your_set)

# Set removes duplicates automatically
your_set = set([1, 2, 2, 5, 5, 6])
print(your_set)

# Union & Intersection
print(my_set.union(your_set))
print(my_set.intersection(your_set))

❗ Note: Sets do not support indexing or slicing, because they are unordered.


🛠️ Mini Python Project: Tuples + Sets Practice

To wrap up the learning, I created a small program that takes a list of items with duplicates and returns:

  1. A tuple of the first 3 unique items

  2. A set of all unique items

✅ The Code:

items = ["apple", "banana", "apple", "orange", "banana", "grape", "apple"]

unique_items = []
for i in items:
    if i not in unique_items:
        unique_items.append(i)

first_three = tuple(unique_items[:3])
all_unique = set(unique_items)

print(f"The first three unique items are: {first_three}")
print(f"The all unique items are: {all_unique}")

🧠 What I Learned

  • How to work with tuples and sets in Python

  • The difference between mutable and immutable types

  • How to filter unique items using basic loops and conditionals

  • How to apply basic set operations


🧡 Final Thoughts

It’s amazing how much you can do with just a few lines of Python. Tuples and sets are simple, but powerful tools in your Python toolkit.

If you're learning Python too, try experimenting with these types and build a little project like I did — it really helps lock in the concepts.

Thanks for reading, and happy coding! ⚡

More from this blog

My Python Journey

24 posts