Set data structure characteristics:
a_set = {'D', 'B', 'M', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S'}
print(a_set) #Set cannot have duplicates
{'I', 'B', 'T', 'A', 'D', 'U', 'L', 'S', 'R', 'M', 'O'}
a_set = {'D', 'B', 'M', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S'}
print(a_set)
Output: {'I', 'B', 'T', 'A', 'D', 'U', 'L', 'S', 'R', 'M', 'O'}
for item in a_set:
print(item)
Output:
I
B
T
A
D
U
L
S
R
M
O
a_set = {'D', 'B', 'M', 'S', 'T', 'U', 'T', 'O', 'R', 'I', 'A', 'L', 'S'}
del a_set
a_set
Traceback (most recent call last):
File "", line 1, in
NameError: name 'a_set' is not defined
set_1 = {1, 2, 3, 6, 'A'}
print(set_1.pop()) # pop() function returning 1
Output: 3
set_1 = {1, 2, 3, 6, 'A'}
print(set_1.remove(2)) # remove() function returning None
Output: None
set_1 = {1, 2, 3, 6, 'A'}
print(set_1.discard(2)) # discard() function returning None
print(set_1.discard(2)) # discard() function returning None
Output: None
set_1 = {i for i in range(0,5)}
print(set_1)
Output: {0, 1, 2, 3, 4}
set_1 = {i for i in range(0,10) if i%3==0}
print(set_1)
Output: {0, 9, 3, 6}
set_1 = {i*j for i in range(1,3) for j in range(2,5)}
print(set_1)
Output: {2, 3, 4, 6, 8}
set_1 = {i*j for i in range(1,3) for j in range(2,5) if i*j//4!=0}
print(set_1)
Output: {8, 4, 6}
set_1 = {1, 2, 3, 6, 'A'}
print(set_1)
Output 1: {1, 2, 3, 6, 'A'}
set_2 = set(set_1) # Creating a copy here
print(set_2)
Output 2: {1, 2, 3, 6, 'A'}
set_2.pop() # Removing item from set_2
Output 3: 1
print(set_2) # 'A' value is not present in set_2
Output 4: {2, 3, 6, 'A'}
print(set_1) # '1' value is still present in set_1
Output 5: {1, 2, 3, 6, 'A'}
Syntax: set_object.copy()
set_1 = {1, 2, 3, 6, 'A'}
set_2 = set_1.copy()
print(set_2)
Output 2: {1, 2, 3, 6, 'A'}
set_2.pop() # Removing item from set_2
Output 3: 1
print(set_2) # '1' value is not present in set_2
Output 4: {2, 3, 6, 'A'}
print(set_1) # '1' value is still present in set_1
Output 5: {1, 2, 3, 6, 'A'}
set_1 = {1, 2, 3, 6, 'A'}
print(set_1)
Output 1: {1, 2, 3, 6, 'A'}
import copy
set_4=copy.copy(set_1) # Creating a copy here
print(set_4)
Output 2: {1, 2, 3, 6, 'A'}
set_4.pop() # Removing item from set_4
Output 3: 1
print(set_4) # '1' value is not present in set_4
Output 4: {2, 3, 6, 'A'}
print(set_1) # '1' value is still present in set_1
Output 5: {1, 2, 3, 6, 'A'}
set_1 = {1, 2, 3, 6, 'A'}
print(set_1)
Output 1: {1, 2, 3, 6, 'A'}
set_5 = set_1 # Creating a reference here
print(set_5)
Output 2: {1, 2, 3, 6, 'A'}
set_5.add('P') # Adding 'P' item to set_5
print(set_5) # 'P' value is present in set_5
Output 4: {'P', 1, 2, 3, 6, 'A'}
print(set_1) # 'P' value is also present in set_1
Output 5: {'P', 1, 2, 3, 6, 'A'}
set_1.pop() # Removing item from set_1
Output 6: P
print(set_1) # 'P' value not present in set_1
Output 7: {1, 2, 3, 6, 'A'}
print(set_5) # '6' value also not present in set_5
Output 8: {1, 2, 3, 6, 'A'}
set_1 = {1, 2, 3, 6, 'A'}
set_2 = {4, 5, 7}
set_1.update(set_2) # Using update() function here
print(set_1) # set_2 elements got added in set_1
Output: {1, 2, 3, 4, 5, 6, 7, 'A'}
set_1.update(5) # Using update() function here
Output: TypeError: 'int' object is not iterable
Output: {1, 2, 3, 5, 6, 'A'}
set_1 = {1, 2, 3, 6, 'A'}
set_2 = {4, 5, 7}
set_1.add(set_2) # Using add() function here
Output: TypeError: unhashable type: 'set'
set_1.add(5) # Using add() function here
print(set_1) # Element 5 got added in set_1
Output: {1, 2, 3, 5, 6, 'A'}