Home>Article>Backend Development> Can elements in a collection be repeated in python?
Elements in a set in python cannot be repeated. A set is an unordered sequence of non-repeating elements. We can use curly brackets {} or the set() function to create a set. The specific syntax is: [parame = {value01, value02,...}].
The elements in a set in python cannot be repeated.
(Recommended learning:python tutorial)
Set definition:
A set (set) is an unordered sequence of non-repeating elements .
Creation:
You can use curly brackets { } or the set() function to create a collection.
Note: To create an empty collection, you must use set() instead of { }, because { } is used to create an empty dictionary.
Format:
parame = {value01,value02,...}
or
set(value)
Example:
jihe = {1,2,3,4,5} #与字典看上去类似,但是是不一样的。字典是 dict1 = {key:value,key2:value2,}需要key
Add elements:
Use set.add() Method
Example:
set1 = {1,3} set1.add(2)print(set1)
The above is the detailed content of Can elements in a collection be repeated in python?. For more information, please follow other related articles on the PHP Chinese website!