Set in Python

Posted on

Introduction

Another collection data type in Python programming language. It is actually have specific characteristics in some comparison with another collection data type such as list, tuple or dictionary. For further information about other collection data types and also other simple data types available in Python programming language, just visit an article exist in this link. That link will direct to another article with the title of ‘Data Type in Python’.

Set Characteristics

In term of collection data type, set has some specific characteristics. Those characteristics make set a little bit different from another collection data type. Those characteristics exist as follows :

  1. Number of data.

    Definitely, as a collection data type, it can store and it can have multiple data available in it. As another collection data type, set can also have multiple data as in the example : {1,2,3,4}

  2. Data types.

    In terms of data type, set can contain or it can store multiple data with various data types. So, each data can have its own data type which different from one another. As an example, in this context : {1,True,”James”,’Australia’,4.97}. The following below is the example to display how a set can be able to contain multiple data with various data types :

    (env) C:\python>python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> set_example = {1,True,"James",'Australia',4.97};
    >>> print(set_example);
    {'Australia', 1, 4.97, 'James'}
    >>> set_example.add(1);
    >>> set_example.add('James');
    >>> set_example.add('Australia');
    >>> set_example.add(1);
    >>> set_example.add(1);
    >>> print(set_example);
    {'Australia', 1, 4.97, 'James'}
    >>>
    
  3. Mutability.

    As for the change to the existing element inside the set, it does not have the ability for it. In other words, set is not mutable or set is immutable. After defining or adding data into it, it will stay and remain unchanged.

  4. Initialization or Definition Format.

    In order to define or to initialize set, it is very easy. It is using the same curly bracket character as dictionary. So, for defining or declaring set, the following is an example :

    set_example = {}
  5. Data Duplication

    As for the duplication aspect, set does not allow any duplication of the element or the data in it. The following is an example with a certain sequence from the declaration with initialization using several data with different data types as follows :

    (env) C:\python>python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> set_example = {1,1,1,1,2,3,4,1,2,3,4}
    >>> print(set_example);
    {1, 2, 3, 4}
    >>>
    

     

One thought on “Set in Python

Leave a Reply