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 2

Strings using python - Other than numbers python can also handle strings which can be expressed by several ways. In programming a string is a contiguous sequence of symbols or values such as a character string or digit string. Strings is declared in a '....' (single quotes) or "...." (double quotes).     By using  '\n'  you can insert the string to the new line by using  print()  command. For example:  We can concatenate string by using + operator. Concatenate means we can merge two or more separate strings by using + operator. For example: We see another example of multiple concatenate. Here 3*'py' means the string 'py' repeats 3 times and same for the 'thon' string. Now we another method to concatenate two or more strings. In this method you have write strings next to each other without any operator. For example: In the python string is stored in indexes. The index is start from 0 (zero). In python indexes also be calculated from reve...

Python | Data Structures or Data Types (List)

Data Type of Python In Python there are two types of data types which are Mutable and Immutable. Mutable data type contains List and Dictionary data types. And Immutable data type contains String,Tuple and Sets data types. Now we see what is Mutable and Immutable data type? The main object get changed permanently after applying methods of data type this data type is called as Mutable data type. And the main object will not be changed permanently after applying methods of data type this data type is called as Immutable data types. Now we see data types one by one! 1) List - List is an ordered collection of mutable datatype. Lists are hashable, iterable. List contain any kind of data. List are declared in square braces[]. Empty List-  L = [ ] List with multiple elements -  L1 = [10, 15.10, 'Python', [10,20] ] In the above list you can add any data type as shown above. In list L1 there is Integer, float,String,list data types. To add string data type you must have to declared in ...

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...