Set

A set in Python is a collection of unique and immutable (unchangeable) objects.

# start with an empty set

my_set = set()

print(my_set)
set()
# add a few elements

my_set.add('a')
my_set.add('b')
my_set.add('c')
my_set.add(1)
my_set.add(2)
my_set.add(3)

print(my_set)
{1, 2, 3, 'b', 'c', 'a'}
for element in my_set:
    print(element)
1
2
3
b
c
a

Last updated

Was this helpful?