In Python, a Set is a collection of unique elements. It is mainly used when we want to store multiple values without duplicates.
A set is based on the mathematical concept of sets.
1. What is a Set?
A set is an unordered collection of distinct, hashable objects.
Property Description
Unordered Does not guarantee insertion order
Unique Elements Duplicate elements are not allowed
No Indexing Cannot access elements using index positions
No Slicing Slicing operations are not supported
No Step Operations Cannot access elements using step-based indexing
Hashable Elements Elements must be hashable
Mutable Elements can be added or removed after creation
Example
s = { 10 , 20 , 30 , 40 } print ( s )
s = { 10 , 10 , 10 , 20 , 30 , 40 } print ( s )
Enter fullscreen mode
Exit fullscreen mode
Output:
{10, 20, 30, 40} {10, 20, 30, 40}
Enter fullscreen mode
Exit fullscreen mode
Even though we provided duplicate values, Python stores only unique elements.
2.…