Set In Python-
- – It is not maintaining insertion order.
- – It doesn’t allow duplicates.
- – It is a mutable collection.
- – Set elements must be mutable.
- – It can store heterogeneous elements.
- – There is no concept of indexing and slicing in set.
Ex -
data = {3, 17, 25.2, "simpleAlgo", 4} print(data) Output- {3, 4, 17, 'simpleAlgo', 25.2}
-
Qsn->
Why set is not having indexing and slicing concept?
-
Ans-
Because set is not maintaining the insertion order so we dont know which elements come at first and which will at last. On every execution sequence of the elements may change.
- Set is mutable, hence we can modify the set after creation as well like list.
Few Of The Predefined Functions-
- add() – It is used to add a values.
- pop() – It is used to remove the value, which value this will pop that is not specify so it can pop/remove any value.
- remove(element) – It is used to remove any specific value.
Ex- For Add()-
data = {3, 17, 25.2, "simpleAlgo", 4} data.add(25) print(data) Output- {3, 4, 17, 25, 'simpleAlgo', 25.2}
Ex- For Remove(Element)-
data = {3, 17, 25.2, "simpleAlgo", 4} data.remove(17) print(data) Output- {3, 4, 25.2, 'simpleAlgo'}
Ex- For Pop()-
data = {3, 17, 25.2, "simpleAlgo", 4} data.pop() print(data) Output- {3, 4, 17, 25.2}
-
- – As set elements are immutable so we can’t store list and set inside the set as both are mutable type collection.
- – We can only store tuple inside the set as tuple is immutable type collection.
-
Qsn-
Why set inside set is also not possible?
-
Ans-
Because set are mutable and we can’t store mutable object inside the set.
Ex-
data = {10, 20, 30, (10, 20)} print(data) data = {10, 20, 30, [10, 20]} #TypeError: unhashable type: 'list' print(data) Output- {10, 20, (10, 20), 30} Traceback (most recent call last): File "E:/Study/Python_BK/SAB/SAB/other/Test.py", line 4, in data = {10, 20, 30, [10, 20]} #TypeError: unhashable type: 'list' TypeError: unhashable type: 'list'
Ex-
l1=[] l2 = list() print(type(l1)) # <class 'list'=""> print(type(l2)) t1 = () t2 = tuple() print(type(t1)) #<class 'tuple'=""> print(type(t2)) s1={} s2=set() print(type(s1)) #<class 'dict'=""> print(type(s2)) #<class 'set'=""> s3={1} print(type(s3))
Ex-
data1 = {10, 20, 30, 40, 50} data2 = {40, 50, 60, 70} print(data1) print(data2) print("\nUnion-->") print(data1.union(data2)) print("\nMathematical function for union by using operators") print(data1 | data2) print("\nDifference-->") print(data1.difference(data2)) print("\nMathematical function for difference by using operators") print(data1-data2) print("\nUn-common element") print(data1.symmetric_difference(data2)) print("\nMathematical function for symmetric difference by using operators") print(data1^data2) print("\nIntersection-->") print(data1.intersection(data2)) print("\nMathematical function for intersection by using operators") print(data1 & data2) Output- {40, 10, 50, 20, 30} {40, 50, 60, 70} Union--> {70, 40, 10, 50, 20, 60, 30} Mathematical function for union by using operators {70, 40, 10, 50, 20, 60, 30} Difference--> {10, 20, 30} Mathematical function for difference by using operators {10, 20, 30} Un-common element {70, 10, 20, 60, 30} Mathematical function for symmetric difference by using operators {70, 10, 20, 60, 30} Intersection--> {40, 50} Mathematical function for intersection by using operators {40, 50}