Header Ads Widget

Responsive Advertisement

Sets in Python3





Keypoints:
       

  • Sets are the unordered sequence of various datatype elements.
  • In sets repetition is not allowed which means every set elements are unique(no duplicates)elements.
  • In sets repetition is not allowed which means every set elements are unique(no duplicates) elements.
  • Sets are immutable which means we can not change the values of sets once they are declared.
  • Sets can have all data type elements but it should not contain mutable elements like lists, sets and dictionaries.
  • Sets are enclosed within curly brackets.
  • Since Sets cannot have repeated values so we can remove duplicate values from list and tuples by just converting them into sets. For E.g.

    my_tuple=(1,3,2,4,5,5)                            # Tuples of repeated elements.
    my_set=set(my_tuple)                              # Converting my_tuple into set.
    print("my_tuple in set format:",my_set)           # It will print the output in set format.
    print("my_tuple in tuple format:",tuple(my_set))  # It will print the output in tuple format.
    OUTPUT:
    my_tuple in set format: {1, 2, 3, 4, 5}
    my_tuple in tuple format: (1, 2, 3, 4, 5)

    Functions associated with Sets: There are many functions associated with sets, few of them are listed below.

    • len()                   
    • add()
    • remove()
    • update()
    • discard()
    • pop()
    • clear()

    len() in sets:    len() function gives the length of a given set that is total no of elements present in a set. For e.g. 

    my_set = {1, 2, 3, (4,2), 12.50}  #Set of integers and float values.
    print(len(my_set))     #Prints total length of the set
    OUTPUT:
    5

    add() in sets: This functions allows us to add elements in our existing set, since sets are sequence of unordered elements so our adding element can be added at any position.
    for e.g. 

    my_set={1, 2, 3, (4,2), 12.50}  #Set of integers,tuples and float values.
    my_set.add('Python')
    
    my_set.add('Program')
    
    print(my_set)
    OUTPUT:
    {1, 2, 3, 12.5, 'Python', (4, 2), 'Program'}

    remove() in sets: It allows to remove a specific element from a set and gives the output in an unordered fashion. For e.g. 

    my_set={1, 2, 3, (4,2), 12.50,'python','Program'}
    
    my_set.remove('Program')  #Removes 'program' from my_set.print(my_set)
    my_set.remove((4,2))   ##Removes (4,2) from my_set.print(my_set)
    OUTPUT:
    {1, 2, 3, 12.5, (4, 2), 'python'}
    {1, 2, 3, 12.5, 'python'}

    update() in sets: It allows to update the existing set with new values by adding iterables, there can be one or more than one iterables.

    syntax:

            set_name.update(iterable)


    example 1:

    my_set={1, 2, 3, (4,2), 12.50,'python','Program'}
    
    my_set.update([4,5],{8,9})
    
    print(my_set)
    OUTPUT:
    {1, 2, 3, 'Program', 4, 5, 8, 9, 'python', 12.5, (4, 2)}
    example 2:
    set1 = {'a', 'b'}
    set2 = {1, 2, 3}
    
    result = set1.update(set2)
    
    print('set1 =', set1)
    OUTPUT:
    set1 = {1, 2, 'a', 'b', 3}

    If dictionary values will passed to update() then only the key values will be printed as output. For e.g. 

    dictionary = {'python': 1, 'program' : 2}
    numbers = {'a', 'b'}
    
    numbers.update(dictionary)
    print('numbers_dictionary =', numbers)
    OUTPUT:
    numbers_dictionary = {'b', 'program', 'python', 'a'}

    discard() in sets: This function has similar functionality with remove() function but the difference is that discard() gives no error if we want to delete an element which is not present in our set, it will print the whole set as it is e.g. 

    my_set={1,2,3,4}
    
    my_set.discard(2)
    print(my_set)
    
    my_set.discard(10)
    print(my_set)
    OUTPUT:
    {1, 3, 4}
    {1, 3, 4}

    pop() in sets: This method removes a random element from give set. We can use this concept to print the deleted element as well as the whole set except deleted element.
    For e.g. 

    s={1,2,3,4}
    
    x=s.pop()
    print("Deleted random element is:",x)
    print("After pop operation the set is:",s)
    OUTPUT:
    Deleted random element is: 1After pop operation the set is: {2, 3, 4}

    clear() in sets: This function makes the set empty.
    e.g.

    my_set= {2, 3, 4}
    
    my_set.clear()
    print(my_set)
    OUTPUT:
    set()














    Post a Comment

    0 Comments