Search⌘ K
AI Features

Basic Set Operations

Explore basic Python set operations to understand mutability, cloning, searching, and identity comparisons. Learn how to convert collections to sets, and grasp key differences like frozenset immutability and the absence of concatenation for sets.

Some basic set operations are given below.

Mutability

Sets are mutable. Their contents can be changed, as shown below.

Python 3.8
s = {'gate', 'fate', 'late'}
s.add('rate') # adds one more element to set s
print(s)

If we want an immutable set, we can use a frozenset.

Python 3.8
s = frozenset({'gate', 'fate', 'late'})
s.add('rate') # error

Other operations

The following are operations that work on lists and tuples. Try these operations on sets in the playground given below:

...