# You can't create a set like this in Python
my_set = {} # ---- This is a Dictionary/Hashmap
# To create a empty set you have to use the built in method:
my_set = set() # Correct!
set_example = {1,3,2,5,3,6}
print(set_example)
# OUTPUT
# {1,3,2,5,6} ---- Sets do not contain duplicates and are unordered
# A set contains unique elements of which the order is not important
s = set()
s.add(1)
s.add(2)
s.remove(1)
print(s)
# Can also be created from a list (or some other data structures)
num_list = [1,2,3]
set_from_list = set(num_list)
myNewSet = set()
myNewSet.add("what have you done")
myNewSet.add("to earn your place")
myNewSet.add("in this crowded world?")
"what have you done" in myNewSet # -> true
myNewSet.remove("to earn your place")
# -> myNewSet = {"what have you done", "in this crowded world?"}
empty_set = set()
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
# sets do not contain duplicates
print(basket)
# output {'orange', 'banana', 'pear', 'apple'}
# check if orange is in basket set
print('orange' in basket)
# output true
# convert a string to a set of letters - sets contains no duplicates
set_a = set('abcd')
set_b = set('bcde')
# the operations below returns new sets
# print letters in set_a but not in set_b - difference
print(set_a - set_b)
# output {'a'}
# print set letters that is in either set a or b - union
print(set_a | set_b)
# output {'a', 'c', 'e', 'b', 'd'}
# print letters that are in both set_a and set_b - intersection
print(set_a & set_b)
# output {'c', 'd', 'b'}
# print letters that are in set_a and set_b when the letters are found in a set but no the other set - symmetric_difference()
print(set_a ^ set_b)
# output {'a', 'e'}
set_of_base10_numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
set_of_base2_numbers = {1, 0}
intersection = set_of_base10_numbers.intersection(set_of_base2_numbers)
union = set_of_base10_numbers.union(set_of_base2_numbers)
'''
intersection: {0, 1}:
if the number is contained in both sets it becomes part of the intersection
union: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}:
if the number exists in at lease one of the sets it becomes part of the union
'''
"""
The Python sets are highly useful to efficiently remove duplicate values from
a collection like a list and to perform common math operations like unions and
intersections.
"""
# Python also has sets which you can use
# Keep in mind that sets cannot have duplicate of the same value
# This can be useful for removing doubles in a list
mySet = {1, 2, 2, 3, 4, 5, 10, 10, 15}
print(mySet) # {1, 2, 3, 4, 5, 10, 15}
# You can also create a set with 'set([list elements])'
myList = ['apples', 'bananas', 'oranges', 'grapes']
myList.append('apples')
myList.append('oranges')
print(myList) # ['apples', 'bananas', 'oranges', 'grapes', 'apples', 'oranges']
myNewSet = set(myList)
print(myNewSet) # {'apples', 'bananas', 'oranges', 'grapes'}
# Python program to demonstrate
# Creation of Set in Python
# Creating a Set
set1 = set()
print("Initial blank Set: ")
print(set1)
# Creating a Set with
# the use of a String
set1 = set("GeeksForGeeks")
print("
Set with the use of String: ")
print(set1)
# Creating a Set with
# the use of Constructor
# (Using object to Store String)
String = 'GeeksForGeeks'
set1 = set(String)
print("
Set with the use of an Object: " )
print(set1)
# Creating a Set with
# the use of a List
set1 = set(["Geeks", "For", "Geeks"])
print("
Set with the use of List: ")
print(set1)
# Python program to demonstrate
# Creation of Array
# importing "array" for array creations
import array as arr
# creating an array with integer type
a = arr.array('i', [1, 2, 3])
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
print (a[i], end =" ")
print()
# creating an array with float type
b = arr.array('d', [2.5, 3.2, 3.3])
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
print (b[i], end =" ")