DEV Community

Aditi Sharma
Aditi Sharma

Posted on

πŸš€ Day 8 of My Python Learning Journey – Sets & Data Structures in Python

Today, I wrapped up learning about Sets and explored more about data structures in Python.

πŸ”Ή What are Sets?
β€’ Unordered collections of unique elements.
β€’ Defined using curly braces {}.
β€’ No duplicates allowed.

fruits = {"apple", "banana", "cherry", "apple"}
print(fruits) # {'apple', 'banana', 'cherry'}

πŸ”‘ Common Set Operations

a = {1, 2, 3}
b = {3, 4, 5}

print(a.union(b)) # {1, 2, 3, 4, 5}
print(a.intersection(b)) # {3}
print(a.difference(b)) # {1, 2}
print(a.symmetric_difference(b)) # {1, 2, 4, 5}

βœ… Sets are very useful for mathematical operations, filtering, and removing duplicates.

πŸ”Ή Why Use Sets?
β€’ Fast membership testing (in operator).
β€’ Automatically remove duplicates.
β€’ Great for mathematical/logical operations.

πŸ”Ή Data Structures Recap in Python

  1. List β†’ Ordered, mutable.
  2. Tuple β†’ Ordered, immutable.
  3. Set β†’ Unordered, unique items.
  4. Dictionary β†’ Key-value pairs.

Each data structure has its own strengths depending on the use case (speed, immutability, uniqueness, etc.).

🎯 Reflection

Sets introduced me to powerful operations for handling unique data. Understanding Python’s core data structures is essential because they form the foundation of problem-solving, algorithms, and real-world applications.

⚑ Next up: I’ll dive into libraries and explore advanced coding practices.

Python #100DaysOfCode #LearningJourney #DataStructures

Top comments (0)