Python set
What is set?
set is a data structure which is also called collection of item in which we can represent a group of unique value as a single entity.
we write the item of set inside the "curly braces {}".
insertion order not preserved.
indexing and & slicing not work.
Heterogenous elements are allowed.
Mutable in nature.
Duplicate are not allowed.
#empty set
var=set()
print(type(var))
#Set with item
var={10,20,50.5,"rishi",True}
print(type(var))
print(var)
#add method for adding one item
var={10,20,50.5,"rishi",True}
var.add("parisa")
print(var)
#update method for updating one or more item
var={10,20,50.5,"rishi",True}
var.update(["parisa","shiansh"])
print(var)
#pop method for deleting item
var={10,20,50.5,"rishi",True}
print(var.pop())
#remove method for deleting particular item
var={10,20,50.5,"rishi",True,"parisa"}
var.remove("parisa")
print(var)
#clear method for clearing full set
var={10,20,50.5,"rishi",True}
var.clear()
print(var)
#union method for printing item or 2 set
var={10,20,50.5,"rishi",True}
var2={10,20,50.5,"parisa","shivansh"}
print(var.union(var2))
# intersection method for printing common item of 2 set
var={10,20,50.5,"rishi",True}
var2={10,20,50.5,"parisa","shivansh"}
print(var.intersection(var2))
#difference method for printing different of 2 sets
var={10,20,50.5,"rishi",True}
var2={10,20,50.5,"parisa","shivansh"}
print(var.difference(var2))
#symmetric difference method is use to print value of 2 sets
var={10,20,50.5,"rishi",True}
var2={10,20,50.5,"parisa","shivansh"}
print(var.symmetric_difference(var2))
Comments
Post a Comment