Monday, 19 February 2018

Python Sets - Day 10(day later)

  • *like dictioanry,but don't have keys
  • *immutable objects
  • *no order
  • *2 ways to create a set,one like the dictioanry,other by using the set keyword
  •   
    farm_animals = {"cow","goat"}
    print(farm_animals)
    wild_animals= set(["Lion","Tiger"])
    farm_animals.add("horse")
    print(farm_animals)
    wild_animals.add("horse")
    print(wild_animals)
    
    evennos = set(range(0,50,2))
    print(evennos)
    
    evennos = set(range(0,50,2))
    print(evennos)
    squares = {4,9,16,25,36}
    print(squares)
    print(sorted(squares))
    print (squares.union(evennos))
    #both will produce same result
    print (evennos.union(squares))
    #both will produce same result
    print (squares.intersection(evennos))
    print (evennos.intersection(squares))
    print (evennos & squares)
    print (squares & evennos )
    
    #*empty set can not be created by using the curly braces {},since it will be treated as 
    dictionary when calling the add method
    emptyset1 = {}
    print(emptyset1)
    #the below code will throw error
    # emptyset1.add("1")
    print(emptyset1)
    emptyset2 = set()
    print(emptyset2)
    #but not the below code
    emptyset2.add("1")
    print(emptyset2)
    
    #differences/Minus
    print(evennos)
    print(squares)
    
    
    print("squares minus evenos")
    print(squares.difference((evennos)))
    print(squares - evennos)
    
    print("evenos minus squares ")
    print(evennos.difference((squares)))
    print(evennos - squares)
    
    #update the difference in the set
    
    squaretuple = (4,9,16,25,36)
    squarenos = set(squaretuple)
    evennos = set(range(0,50,2))
    print(sorted(evennos))
    print(sorted(squarenos))
    print("="*40)
    #will update the set after the difference
    evennos.difference_update(squarenos)
    print(evennos)
    
    
    #symmetric difference,it is exact opposite of intersection
    
    squarenos1 = {4,9,16,25,36}
    evennos1 = set(range(0,50,2))
    print(sorted(squarenos1))
    print(sorted(evennos1))
    
    print(evennos1.symmetric_difference(squarenos1))
    #it will return the same result for both the line of the code
    print(squarenos1.symmetric_difference(evennos1))
    
    #discard & Remove,these two will affect the sets directly,
    #disacard wont throw an error ,even if the value is not present there in the set,
    #where as remove will throw if it does not exits
    
    print(sorted(squarenos1))
    print(sorted(evennos1))
    
    squarenos1.discard(4)
    squarenos1.remove(16)
    print(sorted(squarenos1))
    #wont throw error
    squarenos1.discard(5)
    #will throw error
    # squarenos1.remove(5)
    
    evensquarenos2 = {4,16,36}
    evennos2 = set(range(0,50,2))
    print(sorted(evensquarenos2))
    print(sorted(evennos2))
    
    if evennos2.issuperset(evensquarenos2):
        print("evennos is the superset of the evensquarenos")
    
    if evensquarenos2.issubset(evennos2):
        print("evensquarenos is the subset of the evennos")
    
    
    #frozenset,we can add,edit,remove the frozen set,but can do other operations 
    #like union,intersection...
    evenfrozen = frozenset({2,4,6})
    print(evenfrozen)
    #the below line will throw an error,since we are trying 
    #to modify the frozen set which is not allowed
    # evenfrozen.add(8)
    
    
    #challenge ,remove the vowels from the string
    
    stringval1= set("python is trending language")
    stringval2= set("abcdefghijklmnopqrstuvwxyz")
    frozensetstring = frozenset("aeiou")
    print(stringval1.difference(frozensetstring))
    print(sorted(stringval2.difference(frozensetstring)))