Set Data type
Set is also a collection data type in Python. However, it is not an ordered collection
of objects, like list or tuple. Hence, indexing and slicing operations cannot be done on
a set object. A set also doesn’t allow duplicate objects to be stored, where as in list
and tuple, the same object can appear more than once. Even if an object is put more
than once in a set, only one copy is held. Set is a Python implementation of a set as
defined in Mathematics. The set object has suitable methods to perform mathematical
set operations like union, intersection, difference etc. A set object contains one or
more items, not necessarily of the same type which are separated by comma and
enclosed in curly brackets {}.
>>> S1={1, "Ravi", 75.50}
>>> S1
{1, 75.5, 'Ravi'}
>>> type(S1)
<class 'set'
>>>> S2={10,23,40,23,50,10}
>>> S2
{40, 10, 50, 23}
>>>
set() function
Python has an in-built function set() using which set object can be constructed out of
any sequence like string, list or tuple object.
>>> S1=set("Internshala")
>>> S1
{'t', 'n', 's', 'h', 'e', 'a', 'l', 'I', 'r'}
>>> S2=set([45,67,87,36,55])
>>> S2
{55, 67, 36, 45, 87}
>>> S3=set((10,25,15))
, >>> S3
{25, 10, 15}
>>>
Order of elements in the set is not necessarily the same that is given at the time of
assignment. Python optimizes the structure for performing operations over set as
defined in mathematics. Only immutable (and hashable) objects can be a part of set
object. Numbers (integer, float as well as complex), strings, and tuple objects are
accepted but list and dictionary objects are not.
>>> S1={(10,10), 10,20}
>>> S1
{10, 20, (10, 10)}
>>> S2={[10,10], 10,20}
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
S2={[10,10], 10,20}
TypeError: unhashable type: 'list'
>>>
In first case, (10,10) is a tuple, hence it becomes part of set. In second example though,
since [10,10] is a list, error message is displayed saying list is unhashable. (Hashing is
a mechanism in computer science which enables quicker searching of objects in
computer’s memory. https://en.wikipedia.org/wiki/Hash_function) Even though
mutable objects are not stored in a set, set itself is a mutable object. A set object can
be modified by add(), update(), remove() and discard() methods.
add()
adds a new element in set object
>>> S1=set({"Python", "Java", "C++"})
>>> S1
{'Python', 'C++', 'Java'}
>>> S1.add("Perl")
>>> S1
Set is also a collection data type in Python. However, it is not an ordered collection
of objects, like list or tuple. Hence, indexing and slicing operations cannot be done on
a set object. A set also doesn’t allow duplicate objects to be stored, where as in list
and tuple, the same object can appear more than once. Even if an object is put more
than once in a set, only one copy is held. Set is a Python implementation of a set as
defined in Mathematics. The set object has suitable methods to perform mathematical
set operations like union, intersection, difference etc. A set object contains one or
more items, not necessarily of the same type which are separated by comma and
enclosed in curly brackets {}.
>>> S1={1, "Ravi", 75.50}
>>> S1
{1, 75.5, 'Ravi'}
>>> type(S1)
<class 'set'
>>>> S2={10,23,40,23,50,10}
>>> S2
{40, 10, 50, 23}
>>>
set() function
Python has an in-built function set() using which set object can be constructed out of
any sequence like string, list or tuple object.
>>> S1=set("Internshala")
>>> S1
{'t', 'n', 's', 'h', 'e', 'a', 'l', 'I', 'r'}
>>> S2=set([45,67,87,36,55])
>>> S2
{55, 67, 36, 45, 87}
>>> S3=set((10,25,15))
, >>> S3
{25, 10, 15}
>>>
Order of elements in the set is not necessarily the same that is given at the time of
assignment. Python optimizes the structure for performing operations over set as
defined in mathematics. Only immutable (and hashable) objects can be a part of set
object. Numbers (integer, float as well as complex), strings, and tuple objects are
accepted but list and dictionary objects are not.
>>> S1={(10,10), 10,20}
>>> S1
{10, 20, (10, 10)}
>>> S2={[10,10], 10,20}
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
S2={[10,10], 10,20}
TypeError: unhashable type: 'list'
>>>
In first case, (10,10) is a tuple, hence it becomes part of set. In second example though,
since [10,10] is a list, error message is displayed saying list is unhashable. (Hashing is
a mechanism in computer science which enables quicker searching of objects in
computer’s memory. https://en.wikipedia.org/wiki/Hash_function) Even though
mutable objects are not stored in a set, set itself is a mutable object. A set object can
be modified by add(), update(), remove() and discard() methods.
add()
adds a new element in set object
>>> S1=set({"Python", "Java", "C++"})
>>> S1
{'Python', 'C++', 'Java'}
>>> S1.add("Perl")
>>> S1