Sets
Just like lists and tuples, Python also provides another way to store multiple items: sets. Sets are useful when we want only unique values.
Sets are unordered collections of data items. They store multiple items in a single variable. Set items are separated by commas and enclosed within curly brackets {}. Sets are mutable, meaning you can add or remove items, but you cannot change an existing item because set items must be immutable.
Example:
info = {"Carla", 19, False, 5.9, 19}
print(info)Output:
{False, 19, 5.9, 'Carla'}Here we see that the items of a set occur in random order and hence they cannot be accessed using index numbers. Also, sets do not allow duplicate values.
Accessing set items:
Using a For loop
You can access items of a set using a for loop.
Example:
info = {"Carla", 19, False, 5.9}
for item in info:
print(item)Output:
False
Carla
19
5.9