S
ETS
set1 = {"abc", 34, True, 40, "male"}
print(set1)
print(thisset)
for x in thisset:
print(x)
thisset.add("orange")
print(thisset)
print(set1)
thisset.update(mylist)
print(thisset)
thisset.discard("banana")
print(thisset)
ETS
set1 = {"abc", 34, True, 40, "male"}
print(set1)
{True, 34, 40, 'abc', 'male'}
thisset = set(("apple", "banana", "cherry","apple","Apple"))print(thisset)
{'apple', 'cherry', 'banana', 'Apple'}
print(len(thisset))
4
print(type(thisset))
<class 'set'>
Access Itemsfor x in thisset:
print(x)
apple
cherry
banana
Apple
print("banana" in thisset)cherry
banana
Apple
True
Add,Remove,Join Set Itemsthisset.add("orange")
print(thisset)
{'banana', 'apple', 'orange', 'Apple', 'cherry'}
set1.update(thisset)print(set1)
{True, 34, 'banana', 40, 'abc', 'apple', 'orange', 'Apple', 'male', 'cherry'}
mylist = ["kiwi", "orange"]thisset.update(mylist)
print(thisset)
{'kiwi', 'banana', 'apple', 'orange', 'Apple', 'cherry'}
print(thisset.union(mylist)) this will return new sets
{'kiwi', 'banana', 'Apple', 'apple', 'cherry', 'orange'}
thisset.remove("banana") it will raise an error,if sets has not it.thisset.discard("banana")
print(thisset)
{'kiwi', 'apple', 'orange', 'Apple', 'cherry'}
Comments
Post a Comment