Header Ads

Responsive Ads Here

Sets

Sets



# set --> unordered collection of unique data
s = {1,2,3,4,5,1.1,"1"}
print(s)


l = [2,1,3,4,5,6,7,7,4,4,4,4,4,1,2,3,4]
print(list(set(l))) # order can be changed

# adding data
s.add(6)
print(s)

# removing data
# s.remove(10)
s.discard(10)
print(s)

# clearing sets
# s.clear()
# print(s)

# making a copy of set
# s1 = s.copy()
# s1.add(7)
# print(s1)
# print(s)


# union  ---> |
# intersection -----> &
s1 = {1,2,3,4}
s2 = {3,4,5,6,2,7,8}
print(s1|s2)
print(s1&s2)


if 1 in s:
    print("Present")
else:
    print("Not Present")


for i in s:
    print(i)

Powered by Blogger.