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

Python | Introduction

Python is developed by Guido van Rossum . Guido van Rossum started implementing python in 1989. Python is very simple programming language so if you are new to programming, you can learn python without facing any issue. Features of Python Free and Open Source -                                                                                      Python is a open source language. We can easily install python.It is freely available at python's official website . High level language -                                                                                     ...

Python | Recursive Function

Python - Recursive Function Recursive function means the function call themselves within the function body until a certain condition is matched then this function is recursive function. For example:-  def fact(n):      if n==0 or n==1:           return 1      else:           return n*fact(n-1) print(fact(8)) Output:- 40320 This is the example of recursive function. In this program we create a function named as fact and we call this function within the same function. How this program works? We give the value of n is 8 so n=8. In the first if condition, 8 is not equal to 0 and 1 so it goes to the else part in the else part the sentence is return n*fact(n-1) that means 8*fact(7) now here value of n become 7 to find fact(7) again same procedure repeat up to fact(1).   Because fact(1)=1   we give this condition. Then  fact(2) => 2*fact(1) =>  2*1 =>  2   ...

Python | Control Flow Tools

Control Flow Tools In this session we will see about control flow tools of python. 1) if Statement:- In the programming language if statement is important statement. Now we see how to write if statement in the python program. For Example:- >>> x = int(input("Please Enter an integer")) Please Enter an integer 50 >>> if x<0:      x=0      print("Negative change to zero") elif x==0: print("Zero") elif x==1: print("One") else: print("More") More >>>  Here is the program of if statement. You can add one or more elif part in the program. else part is the optional.      In the program we take the value of x is 50. In the if statement we write x<0 condition which is false because 50 is not less than 0 so it will go to the next part of the code which is first elif part, here we give condition x==0 this condition is also false so it will go to the second elif part which is x==1 this condition is also f...