集合中元素的顺序是未定义的,尽管它可能包含各种元素。可以添加和删除集合的元素,可以迭代集合的元素,可以对集合执行各种标准操作(并集,交集,差异)。通过将内置set()
函数与可迭代对象或序列一起使用来创建集合,方法是将序列放在花括号内,用“逗号”分隔:
# Python program to demonstrate
# Creation of Set in Python
# Creating a Set
set1 = set()
print("Intial blank Set: ")
print(set1)
# Creating a Set with
# the use of a String
set1 = set("GeeksForGeeks")
print("\nSet with the use of String: ")
print(set1)
# Creating a Set with
# the use of Constructor
# (Using object to Store String)
String = 'GeeksForGeeks'
set1 = set(String)
print("\nSet with the use of an Object: " )
print(set1)
# Creating a Set with
# the use of a List
set1 = set(["Geeks", "For", "Geeks"])
print("\nSet with the use of List: ")
print(set1)
# Creating a Set with
# a List of Numbers
# (Having duplicate values)
set1 = set([1, 2, 4, 4, 3, 3, 3, 6, 5])
print("\nSet with the use of Numbers: ")
print(set1)
# Creating a Set with
# a mixed type of values
# (Having numbers and strings)
set1 = set([1, 2, 'Geeks', 4, 'For', 6, 'Geeks'])
print("\nSet with the use of Mixed Values")
print(set1)








暂无数据