Sets
Explore the concept of sets in Python, learning their key properties like uniqueness and unordered elements. Understand how to create sets, perform mathematical operations, and manipulate set elements to handle collections without duplicates.
Definition
A set is a collection of values that are not necessarily all the same type. To write a set, use braces around a comma-separated list. Take a look at a few examples using sets below:
Empty set
In Python, an empty set (one with no elements) cannot be written as {}. Instead, the set() function is used.
Set properties
Sets have two important properties:
- Sets have no duplicate elements. A value is either in the set, or it is not in the set. Sets provide the
inandnot inoperators which allow us to check duplicates.
- The order of elements in a set is unspecified, so you cannot index