Introduction
Python sets are a built‑in data structure used to store collections of unique elements. They
are commonly used when programmers need to remove duplicate values or perform
mathematical set operations such as union and intersection.
Unlike lists and tuples, sets do not maintain the order of elements. This means that elements
are stored in an unordered manner and cannot be accessed using indexing. Despite this
limitation, sets are extremely useful when dealing with large collections of unique data.
In modern programming, sets are widely used in data processing, filtering duplicate
records, and performing operations that require comparison between multiple collections.
Definition
A Python set is an unordered collection of unique elements. Sets are defined using curly
braces {} or by using the set() constructor.
For example: numbers = {1,2,3,4}. In this example, a set named numbers contains four
unique integer values.
If duplicate elements are added to a set, Python automatically removes the duplicates,
ensuring that every element appears only once.
Key Characteristics of Sets
Sets are unordered collections of elements.
Sets do not allow duplicate values.
Sets are mutable, meaning elements can be added or removed.
Sets do not support indexing or slicing because they are unordered.
Sets support mathematical operations such as union, intersection, and difference.
Creating Sets in Python
A set can be created by placing elements inside curly braces.
For example: fruits = {'apple', 'banana', 'orange'}.
Another method is using the set() function such as: numbers = set([1,2,3,4]).