1thisset = {"apple", "banana", "cherry"}
2
3if "apple" in thisset:
4 print("Yes, 'apple' is in this set")
1# You can't create a set like this in Python
2my_set = {} # ---- This is a Dictionary/Hashmap
3
4# To create a empty set you have to use the built in method:
5my_set = set() # Correct!
6
7
8set_example = {1,3,2,5,3,6}
9print(set_example)
10
11# OUTPUT
12# {1,3,2,5,6} ---- Sets do not contain duplicates and are unordered
13
1>>> A = {0, 2, 4, 6, 8};
2>>> B = {1, 2, 3, 4, 5};
3
4>>> print("Union :", A | B)
5Union : {0, 1, 2, 3, 4, 5, 6, 8}
6
7>>> print("Intersection :", A & B)
8Intersection : {2, 4}
9
10>>> print("Difference :", A - B)
11Difference : {0, 8, 6}
12
13# elements not present both sets
14>>> print("Symmetric difference :", A ^ B)
15Symmetric difference : {0, 1, 3, 5, 6, 8}
16
1set_example = {1, 2, 3, 4, 5, 5, 5}
2
3print(set_example)
4
5# OUTPUT
6# {1, 2, 3, 4, 5} ----- Does not print repetitions