Skip to main content

Python | Data Structure Or Data Types (Sets)

Data Type Of Python

In this section we will see new data type which is Sets.

3) Sets:- Set object is an unordered collection of distinct unhashable object. The common user include membership test removing duplicate from a sequence and computing mathematical operation such as intersection, union, differences etc.
    Being an unordered collection set do not record element position or order of insertion. Accordingly set do not support indexing, slicing, stepping or other sequence like behavior. 
    We declare a set by using curly braces {}.
Note:- Set can only store immutable data.

  • Empty set:- 
    S = set()

  • Set with multi element:-
    S = {10, 'Hi', (10, 20)}

  • Membership:-
    print(10 in S) => True
    print(20 in S) => False

  • Iteration:-
    for i in S:
          print(i) 
    => 10
          'Hi'
          (10,20)

  • Type casting:-
    S = "Programming"
    S2 = set(S)
    print(S2) => {'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n'}
    In the above example we store our string Programming in variable S. And in variable S2 we convert this String into Set. While converting to the Sets it removes duplicate characters. In Programming there is g, m, r characters are comes multiple time but when it goes to the Sets it accepts only one character from it and remaining characters will be remove.

Methods Of Sets:-

  1. add(element):- It adds an element to a set. This method has no effect if the element is already present in a set.
    For example:-
    S = {'p', 'r', 'g' } 
    S.add('q') => { 'p', 'r', 'q', 'g' }
    S.add('p') => { 'p', 'r', 'q', 'g' }

  2. clear():- It removes all elements of a set and make it empty.
    print(S.clear()) => set()

  3. difference(-):- It returns the difference of two or more set as a new set. 
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    S3 = { 'x', 'y', '10' }
    print(S2.difference(S3)) => {'p', 'w', 'o'}
    In the above example it does not accept common element from two sets. It accepts only unique elements from the sets.
    You can also write,
    print(S2-S3) => { 'p', 'w', 'o'}
    It also returns the same result.

  4. difference_update(-=):- It removes all elements of another set from current set.
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    S3 = { 'x', 'y', '10' }
    print(S2.difference_update(S3)) => S2 = { 'p', 'w', 'o' }
    This example returns same result of difference method. But one change is in difference_update method the current set will be change or update with output. And difference method returns new set.

  5. discard(element):- It removes an element from a set if it is a member of set. If the element is not the member it does nothing.
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    S3 = { 'x', 'y', '10' }
    S3.discard('y') => S3 = { 'x', 10}
    S2.discard('A') => (It does nothing so you did not get any result)

  6. remove(element):- It remove element from a set if it is a member of set. If the element is not a member it raises KeyError.
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    S3 = { 'x', 'y', '10' }
    S1.remove('p') => { 'r', 'g' }
    S1.remove('q') => KeyError

  7. Intersection(&):-  It returns the intersection of a two sets as a new set. That means it returns common element from both sets.
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    S3 = { 'x', 'y', '10' }
    print(S1.intersection(S2)) => { 'p' }
    Here p is common element in both the sets.
    print(S2&S3) => { 'x', 'y' }

  8. intersection_update(&=):- It update current set with a intersection of itself and another.
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    S3 = { 'x', 'y', '10' }
    print(S1.intersection_update(S2)) => S1={ 'p' }
    Here set S1 is update with the output of above expression.

  9. isdisjoint(set):- It returns true if two sets have null intersection.
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    S3 = { 'x', 'y', '10' }
    print(S1.isdisjoint(S3)) => True
    Here in S1 and S3 set does not have any common element that means it have null intersection so it return true. Now we see another example.
    print(S1.isdisjoint(S2)) => False
    Here in S1 and S2 set p is a common element so it not have null intersection, so it return false.

  10. issubset(set):- It return true if another set contains current set otherwise false.
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    S3 = { 'x', 'y', '10' }
    S4 = { 'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n' }
    print(S1.issubset(S4)) => True.
    Here all elements of S1 set are present in the S4 set so S1 is the subset of S4.
    print(S1.issubset(S2)) => False.
    Here not all elements of set S1 present in the set S2 so it return false.

  11. issuperset(set):- It return true whether current set contain another set.
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    S3 = { 'x', 'y', '10' }
    S4 = { 'p', 'r', 'o', 'g', 'a', 'm', 'i', 'n' }
    print(S3.issuperset(S4)) => False.
    Here set S3 does not have all elements in S4 so it return False.
    print(S4.issuperset(S1)) => True.
    Here set S4 have the elements which are also in set S1.

  12. pop():- It remove and return arbitary set element, Raises key error if set is empty.
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    S3 = { 'x', 'y', '10' }
    print(S3.pop()) =>10
    print(S3.pop()) => 'x'
    print(S3.pop()) => 'y'
    print(S3.pop()) => KeyError
    Here it remove any element. Set not have index number so it remove any element of any position.

  13. symmetric_difference(set):- It return the symmetric difference of two set as a new set. It remove same elements from both set and return the new set with the remaining all elements of both the sets.
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    print(S1.symmetric_difference(S2)) => {'o', 'x', 'g', 'y', 'r', 'w'}

  14. symmetric_difference_update(set):- It return the symmetric difference of two set with the update of current set.
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    print(S1.symmetric_difference_update(S2)) => S1 = 
    {'o', 'x', 'g', 'y', 'r', 'w'}
    Here set S1 update with the result of symmetric difference. It is similar to the symmetric_difference but symmetric_difference_update change current set.

  15. union(set):-  It return the union of set as a new set. That means it returns new set containing all elements of both sets.
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    S3 = { 'x', 'y', '10' }
    print(S1.union(S3)) => { 'p', 'r', 'g', 'x', 'y', '10' }

  16. update(set):- It update a current set with union of itself with other.
    For example:-
    S1 = { 'p', 'r', 'g' }
    S2 = { 'p', 'w', 'x', 'y', 'o' }
    S3 = { 'x', 'y', '10' }
    print(S1.union(S3)) => S1 = { 'p', 'r', 'g', 'x', 'y', '10' }
    Here S1 is update with union of set S1 and S3.

Here we see all the methods of set data type. 

Comments

Popular posts from this blog

Basic Knowledge of Python part 1

Basic Mathematics equations using Python- As we know python is a simple and easy to learn programming language. In this section we learn how to done mathematics equations using python. To perform this we use our basic operators +,-,/,* .  To perform python we have to open python interpreter. In this interpreter you can type simple equation like 5+5, 4/2 etc. For example: The integer number (e.g. 1,2,3....) have type int and the number with fractional part (e.g. 2.5,2.1,4.5...) have type float. Note:-  The division type always returns float. To get only an integer result of division you can use the // operator and to get remainder from division result you can use % operator. In python you can easily calculate power of the number by using ** operator. Like this you can easily calculate mathematics equation. Now we see how to find area of rectangle. Area = length*width is the formula to find area of rectangle. Here '=' sign is used to store value to the variable. You can als...

Python | Data Structure or Data Types (String)

Data Type Of Python I n the last session we discus about what is the data type of python. And we see List data type. In this session we s ee the next data type String.     2) String- String is a ordered collection of immutable data type. String are iterable and hashable. Strings are always declared in single or double quotes.  Single line string:- S = "Python" Multi line string:- S = """ Line 1             Line 2             Line 3 """ Iterable:- S= "Program" for i in S:       print(i) Output:-  P  r  o  g  r  a  m Membership:-  print( 'p' in S) => True print( 'P' not in S) => False Methods of string:- capitalize() :-  It return the capitalize version of the string. More specifically it make the first character have uppercase and rest lowercase. For example:- S = "hello World" print(S.capitalize()) => "Hello world" count(substring, start,en...

Python | Data Structure Or Data Type (Tuple)

Data Type Of Python  In this session we will see last data type of python which is Tuple. 5) Tuple:- Tuple is a ordered collection of immutable datatype, tuples are quite similar to lists but difference is list are mutable where as tuple are immutable. Empty tuple:-  t = () or tuple() Tuple with single element:- t = (10,) Tuple with multiple element:- t1 = ( 10, 20, 30, 'Hello' ) Nested tuple:-  t1 = ( 10, 20, 30, 'Hello' ) p = t2, 30, 40, 50 print(p) => ((10, 20, 30, 'Hello'), 30, 40, 50) Methods of Tuple:-   count(value):- It return the count of value if the value is present in the tuple else it return 0. For example:- t = (10, 20, 30, 'Hello') t.count(10) => 1  t.count(50) => 0 index(value,start,end):- It return the index number if the value is present in the tuple, otherwise it raised value error. For example:- t = (10, 20, 30, 'Hello') t.index(10) => 0 It return the index number of value 10. Prev Topic   Next Topic