- MysGln 的博客
Programming Slide on 13 August
- @ 2024-8-13 16:26:39
Code
demo1.py
# use {} create a set
# setname = {element1, element2, ... elementN}
# For example
S1 = {1,2,3,3,4,4}
print(S1)
# use set() create a set
# Convert iterable objects such as strings, lists, tuples, and range objects
# into set
# If you want to create an empty set, you can only use the set() function to do so.
S2 = set([1,2,3,4,4,4])
print(S2)
demo2.py
S = set([1,2,3,4,5,5,6,6,7,8,9,9])
for i in S:
print(i, end=' ')
demo3.py
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.update(y)
print(x)
demo4.py
thisset = {"apple", "banana", "cherry"}
thisset.update("apple")
print(thisset)
demo5.py
thisset = {"apple", "banana", "cherry"}
thisset.add("apple")
print(thisset)
demo6.py
thisset = {"apple", "banana", "cherry"}
thisset.add("apple")
print(thisset)
demo7.py
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits)