❮ Previous Next ❯

Python Sets

Python have different data structures to store and organize data like List, Set, Tuple, Dictionary, etc. Now we will discuss about sets.
Now we will begin by creating an empty set.

>>> example = set()
>>> example
set()

To see all functions in a set we write
dir(example)
pythonsets

pythonsets_pic



Now we want to insert element in set, so we call add() function. If we will call help() function, like

>>> help(example.add)

Help on built-in function add:

add(...) method of builtins.set instance
    Add an element to a set.

This has no effect if the element is already present.
Here we got clarity using add() , we can insert many types of data items.

>>> example.add(50)

>>> example.add(True)

>>> example.add(3.14159)

>>> example.add("Sashi")

>>> example

{True, 3.14159, 100, 'Sashi', 50}


So here many types of item that we have inserted. Each item in Set is called as element.
Suppose if we want to insert a same data item let 50, then one time it will be assigned, like

>>> example.add(50)
>>> example

{True, 3.14159, 100, 'Sashi', 50}

To see the number of elements in Set, we use len() function

>>> len(example)

5


To remove an element we use remove() function. Before that we will see the help desk

>>> help(example.remove)

Help on built-in function remove:

remove(...) method of builtins.set instance
Remove an element from a set; it must be a member



If the element is not a member, raise a KeyError.
To remove an element 50, then we write

>>> example.remove(50)

>>> len(example)

4

>>> example

{True, 3.14159, 100, 'Sashi'}

Then if we want to remove an element which is not present in Set, we will get error like:

>>> example.remove(200)


Traceback (most recent call last):

File "<pyshell#18>", line 1, in <module>

   example.remove(200)

KeyError: 200


There is another approach to remove element from a Set, that is by using discard() method.
For example;

>>>example.discard(300)

>>>


Here the element 300 not present in Set, but calling discard() function, no error arise. Simply there is nothing.
There is a second way to create a Set, like:

>>> example2=set([30,"Sashi",True,3.141])

>>> len(example2)

4

>>> example2

{'Sashi', True, 3.141, 30}

To create an empty set from the above set, we call clear() function.

>>> example2.clear()

>>> len(example2)

0

There is nothing to see here.
Now we will see some operations on Set like Union, Intersection etc.

>>> odds=set([1,3,5,7,9])

>>> evens=set([2,4,6,8,10])

>>> primes=set([2,3,5,7])

>>> composites=set([4,6,8,10])

>>>

>>> odds.union(evens)

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

>>> evens.union(odds)

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

>>> odds

{1, 3, 5, 7, 9}

>>> evens

{2, 4, 6, 8, 10}

>>> odds.intersection(primes)

{3, 5, 7}

>>> primes.intersection(evens)

{2}

>>> evens.intersection(odds)

set()
>>> primes.union(composites)

{2, 3, 4, 5, 6, 7, 8, 10}


Another operation in set that we can check element from a set whether it is True or False.
For example;
>>> 2 in primes

True

>>> 6 in odds

False

>>> 11 not in evens

True


❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved