How to use all set methods in Python

1 Answer

0 votes
st = {"python", "php", "java"}

for item in st:
    print(item)
print("\n")

if "python" in st:
    print("2.", "Yes")

print("3.", len(st))

st.add("c#")
print("4.", st)

st.update(["c++", "vb"])
print("5.", st)

st.remove("c++")
print("6.", st)

st.discard("c#")
print("7.", st)

s = st.pop()
print("8.", s)
print("9.", st)

st1 = st.copy()
print("10.", st1)

st1.clear()
print("11.", st1)

st = {"python", "php", "java"}
st1 = {"python", "php", "c++"}
s = st.difference(st1)
print("12.", s)
print("13.", st)
print("14.", st1)

s = st.difference_update(st1)  # removes items that exist in both sets
print("15.", s)
print("16.", st)
print("17.", st1)

st = {"python", "php", "java"}
st1 = {"python", "php", "c++"}
s = st.intersection(st1)
print("18.", s)  # returns a set that contains the similarity between two sets

s = st.intersection_update(st1)  # Removes items that are not present in other set
print("19.", s)
print("20.", st)
print("21.", st1)

st = {"python", "php", "java"}
st1 = {"c#", "vb", "c++"}
s = st.isdisjoint(st1)  # returns True if none of the items are present in both sets
print("22.", s)

st = {"python", "php"}
st1 = {"python", "php", "c#", "vb", "c++"}
s = st.issubset(st1)  # returns True if all items in the set exists in other set
print("23.", s)

st = {"python", "php", "c#", "vb", "c++"}
st1 = {"python", "php"}
s = st.issuperset(st1)  # returns True if all items in the specified set exists in the original set
print("24.", s)

st = {"python", "php", "c#", "vb", "c++"}
st1 = {"python", "php", "objective‑c"}
s = st.symmetric_difference(st1)  # returns set that contains all items from both set, that are not present in both sets
print("25.", s)

st = {"python", "php", "c#", "vb", "c++"}
st1 = {"python", "php", "objective‑c"}
s = st.symmetric_difference_update(st1)  # updates original set by removing items that present in both sets, and inserting the others
print("26.", s)
print("27.", st)
print("28.", st1)

st = {"python", "php", "c#", "vb"}
st1 = {"python", "php", "c++"}
s = st.update(st1)  # Update set with the union of the set and others
print("29.", s)
print("30.", st)
print("31.", st1)

st = {"python", "php", "c#", "vb"}
st1 = {"python", "php", "c++"}
s = st.union(st1)  # Return the union of sets
print("32.", s)

del st
# print("33.", st) # Error: name 'st' is not defined


'''
run:
 
php
java
python


2. Yes
3. 3
4. {'php', 'c#', 'java', 'python'}
5. {'php', 'java', 'python', 'vb', 'c#', 'c++'}
6. {'php', 'java', 'python', 'vb', 'c#'}
7. {'php', 'java', 'python', 'vb'}
8. php
9. {'java', 'python', 'vb'}
10. {'java', 'python', 'vb'}
11. set()
12. {'java'}
13. {'php', 'java', 'python'}
14. {'php', 'c++', 'python'}
15. None
16. {'java'}
17. {'php', 'c++', 'python'}
18. {'php', 'python'}
19. None
20. {'php', 'python'}
21. {'php', 'c++', 'python'}
22. True
23. True
24. True
25. {'objective‑c', 'vb', 'c#', 'c++'}
26. None
27. {'objective‑c', 'vb', 'c#', 'c++'}
28. {'php', 'objective‑c', 'python'}
29. None
30. {'php', 'c++', 'python', 'vb', 'c#'}
31. {'php', 'c++', 'python'}
32. {'php', 'c++', 'python', 'vb', 'c#'}

'''

 



answered Nov 22, 2018 by avibootz

Related questions

1 answer 135 views
1 answer 165 views
165 views asked Nov 22, 2018 by avibootz
1 answer 158 views
158 views asked Nov 22, 2018 by avibootz
...