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
131basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
2print(basket) # show that duplicates have been removed
3# OUTPUT {'orange', 'banana', 'pear', 'apple'}
4print('orange' in basket) # fast membership testing
5# OUTPUT True
6print('crabgrass' in basket)
7# OUTPUT False
8
9# Demonstrate set operations on unique letters from two words
10
11print(a = set('abracadabra'))
12print(b = set('alacazam'))
13print(a) # unique letters in a
14# OUTPUT {'a', 'r', 'b', 'c', 'd'}
15print(a - b) # letters in a but not in b
16# OUTPUT {'r', 'd', 'b'}
17print(a | b) # letters in a or b or both
18# OUTPUT {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
19print(a & b) # letters in both a and b
20# OUTPUT {'a', 'c'}
21print(a ^ b) # letters in a or b but not both
22# OUTPUT {'r', 'd', 'b', 'm', 'z', 'l'}1
2SET: Can only store unique values,
3 And does not maintain order
4- HashSet can have null, order is not guaranteed
5- LinkedHashSet can have null and keeps the order
6- TreeSet sorts the order and don't accept null
7
81Set<String> names = new HashSet<>();
2names.add("John");
3names.add("Jack");
4names.add("John");
5System.out.println(names); // [John, Jack]
6
7names.remove("John");
8
9boolean contains = names.contains("Jack"); // true
10
11for (String name: names) {
12 System.out.println(name);
13}1@echo off
2set a[0]=1
3set a[1]=2
4set a[2]=3
5Rem Setting the new value for the second element of the array
6Set a[1]=5
7echo The new value of the second element of the array is %a[1]%