Q1 :-Why indexing and slicing are not possible in set ??

Q2: Are Mathematics Operator like (+ ,-,*) are not possible with set ?

Q1 :-Why indexing and slicing are not possible in set ??

Q2: Are Mathematics Operator like (+ ,-,*) are not possible with set ?

**Q1 Why indexing and slicing are not possible in set?**

- A set contains only unique elements but at the time of set creation, multiple duplicate values can also be passed.

2.Order of elements in a set is undefined and is unchangeable.

For example,

```
set1 = set([1, 2, 4, 4, 3, 3, 3, 6, 5])
print(set1)
set2 = {4,7,8,3,2,1}
print(set2)
set3 = set()
# Adding element and tuple to the Set
set3.add(8)
set3.add(9)
set3.add((6, 7))
print("\nSet after Addition of Three elements: ")
print(set3)
for i in range(1, 6):
set3.add(i)
print("\nSet with the use of List: ")
print(set3)
```

```
set1: {1, 2, 3, 4, 5, 6}
set2: {1, 2, 3, 4, 7, 8}
Set3 after Addition of Three elements:
{8, 9, (6, 7)}
Set3 with the use of List:
{1, 2, 3, (6, 7), 4, 5, 8, 9}
```

As we can see, for ordered input(set1), the output is fine.

But, for set2 (unordered set) and set3 (set with insertion/deletion operation) the input and output order may change.

So, when different people perform same operation on set, may get different ordered set as output.

Therefore, indexing and slicing also can result in two different outputs and can cause error.

so, they are not supported for set.

**Q2. Are Mathematics Operator like (+ ,-,*) are not possible with set ?**

because of behaviour of set mentioned above, arithmetic operators can cause same issue as

indexing and slicing, so they are not supported.

Instead, set supports functions like union, intersection ,difference, subset, superset etc. where order in the input/output is not important. Instead, this functions only provides data which satisfies the conditionâ€¦

For example,

```
set1 = set([1, 2, 4, 4, 3, 3, 3, 6, 5])
set2 = {4,7,8,3,2,1}
set3 = {1, 2, 3, (6, 7), 4, 5, 8, 9}
print("Union of set1 and set2:",set2.union(set1))
print("intersection of set1 and set3",set1.intersection(set3))
print('difference between set2 and set3',set2.difference(set3))
```

```
Union of set1 and set2: {1, 2, 3, 4, 5, 6, 7, 8}
intersection of set1 and set3 {1, 2, 3, 4, 5}
difference between set2 and set3 {7}
```

1 Like

But we can perform â€ś-â€ť operator in set ie

s = {1,123,456,10}

s1= {10,456}

print(s-s1)

2 Likes

yes, I mentioned about difference but forgot to add use of â€ś-â€ť sign.

Thank you.

1 Like