Can elements in a collection be repeated in python?

王林
Release: 2020-07-18 10:28:56
Original
19148 people have browsed it

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,...}
Copy after login

or

set(value)
Copy after login

Example:

jihe = {1,2,3,4,5}
#与字典看上去类似,但是是不一样的。字典是 dict1 = {key:value,key2:value2,}需要key
Copy after login

Add elements:

Use set.add() Method

Example:

set1 = {1,3}
set1.add(2)print(set1)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template