Limited Offer
60% OFF on all plans!

Set in Python

Master Python sets: creation, uniqueness behavior, CRUD operations, mathematical set operations, performance characteristics, frozen sets, comprehensions, and real-world use cases.

Introduction to Sets

A set is an unordered collection of unique, hashable elements.

1. What is a Set?

Unique unordered collection.

2. Duplicate Removal

Duplicates are ignored.

Creating Sets

Sets can be created using literals or the set() constructor.

1. Creating an Empty Set

Use set(), not {}.

2. Creating Set from Iterable

Convert list to set.

Basic Set Operations

Add, remove, check membership.

1. Adding Elements

Use add().

2. Removing Elements

Use remove() or discard().

3. Membership Check

Use in operator.

4. Clearing Set

Use clear().

Mathematical Set Operations

Python provides built-in mathematical operators.

1. Union

Use | operator.

2. Intersection

Use & operator.

3. Difference

Use - operator.

4. Symmetric Difference

Use ^ operator.

Subset & Superset Checks

Compare relationships between sets.

1. Subset

Use <= operator.

2. Superset

Use >= operator.

Set Comprehension

Create sets dynamically.

1. Basic Set Comprehension

Transform values.

2. Conditional Set Comprehension

Filter values.

Frozen Set

Immutable version of a set.

1. Creating frozenset

Immutable set.

Performance & Internals

Sets use hash tables internally.

1. Time Complexity

Average case performance.