Home>Article>Backend Development> Can elements in a collection be repeated in python?

Can elements in a collection be repeated in python?

王林
王林 Original
2020-07-18 10:28:56 19073browse

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,...}].

Can elements in a collection be repeated in python?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn