You are on page 1of 3

1.

Creating set and adding elements in it

myset = {“pink”, “green”, “yellow”, 6, 3, 4.8}

print(myset)

2.Identical elements from two sets

myset1 = {“pink”, “green”, “yellow”, 6, 3, 4.8}

myset2 = {“red”, “yellow”, “green”, 5, 3, 2.6}

print(myset1.intersection(myset2))

3.Unique elements from two sets

myset1 = {“pink”, “green ”, “yellow”, 6, 3, 4.8}

myset2 = {“red”, “yellow”, “green”, 5, 3, 2.6}

print(myset1.union (myset2))

4.Update the first set with elements that don’t exist in second set

Myset1 = {1, 2, 3, 4, 5}

Myset2 = {4, 5, 6, 7, 8}

Myset1.update(myset2 -myset1)

Print(myset1)

5.Remove items from the set at once

myset1 = {1, 2, 3, 4, 5}

items_to_remove = {4, 5}

myset1.difference_update(items_to_remove)

print(myset1)

6.Return set of elements present in set A or set B but not in both


A = {1, 2, 3, 4, 5}

B = {4, 5, 6, 7, 8}

print(A.union(B))

7.Check if two sets have common items, if yes display them

myset1 = {1, 2, 3, 4, 5}

myset2 = {9, 8, 7, 6, 5}

myset3 = myset1.intersection(myset2)

if (myset1.intersection(myset2)) != {}:

print(“Common items:”,myset3)

8.Update set1 by adding items from set2 except common onces

myset1 = {1, 2, 3, 4, 5, 6}

myset2 = {6, 7, 8, 9, 0, 1}

myset1.update(myset2)

print(myset1)

9.Remove items from set1 that are not common in both set1 and set2

myset1 = {1, 2, 3, 4, 5, 6}

myset2 = {6, 7, 8, 9, 0, 1}

myset2.difference(myset1)

print(myset2)

You might also like