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
- List β Ordered, mutable.
- Tuple β Ordered, immutable.
- Set β Unordered, unique items.
- 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.
Top comments (0)