Home > Article > Backend Development > Python zero-based introduction to ten collections
First remember several characteristics of sets: sets are unordered, and the elements in the set are unique.
1. The definition of a set
The definition of a set is very similar to a dictionary, except that the key-value pairs in the dictionary are converted into elements.
num={1,2,3,4,5} print(type(num)) print(num) print("\n") #集合会自动剔除重复的数据,并且集合也是无序的 num2={1,2,3,4,5,5,4,32} print(num2) print("\n")
#2. Creation of collections
(1) Use the definition method
(2) Use the set() factory function
set1=set([1,2,3,4,5,6]) print(set1)
3. Commonly used functions of collections
(1)add() function: Add functions to the collection
(2)remove() function: Remove elements from the collection
( 3) Use frozenset() to create an unmodifiable collection: Once a collection is created, the collection cannot be modified, otherwise an exception will occur
num3=frozenset([1,2,3,4,5]) num3.add(6)
The above is a zero-based introduction to Python Ten collections of content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!