Sets

A set (https://docs.python.org/3/tutorial/datastructures.html#sets) is an unordered collection of immutable elements without duplicate entries. A set could be created as follows:

    >>> my_set = set([1, 2, 3, 4, 5])

>>> my_set

{1, 2, 3, 4, 5}

Now, let's add a duplicate list to this set:

    >>> my_set.update([1, 2, 3, 4, 5])

>>> my_set

{1, 2, 3, 4, 5}

Sets enable avoid duplication of entries and saving the unique entries. A single element can be added to a set as follows:

    >>> my_set = set([1, 2, 3, 4, 5])

>>> my_set.add(6)


>>> my_set


{1, 2, 3, 4, 5, 6}

Sets are used to test memberships of an element among different sets. There are different methods that are related to membership tests. We recommend learning about each method using the documentation on sets (run help(my_set) to find the different methods available for membership tests).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.150.59