What is a set?
We can look at
sets (type
set) as
collections from mathematics. The elements of such a data type are
unordered (we cannot use indices),
and each one is
unique (it appears only once). However, it is allowed to add or remove elements.
To create
a set of data, we use
curly braces:
DETAILS
The built-in function
len() also specifies the number of elements in this case,
and to completely delete a set, we use the keyword
del followed by the variable name, as we are accustomed to.
Additionally, with the
in operator and the
not in group, we can
check if an object is present among its elements or not.
A set can contain objects of different types, as with other data collections.
The variable
set2 holds two real numbers (
float), an integer (
int), and a tuple (
tuple) with two string elements (
str).
Important Note. After forming a set, Python does not maintain the order of the elements as they were introduced, as you can see,
suggesting the absence of indexes for access.
Proceed to the next page.