Python 3


"The first Python course that simply amazed me. Very well explained and easy to understand." (Alexandru Cosmin)

"The best Python course in Romania." (Iulian Geană)

PAGE 1 / 4
Sets in Python
Home >>> Online Lessons, Python 3

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:
Editor -
Console/Output done

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.
 home   list  CONTENTS   perm_identity   arrow_upward