Home > Backend Development > Python Tutorial > What type is python set?

What type is python set?

藏色散人
Release: 2019-08-01 16:16:00
Original
13491 people have browsed it

What type is python set?

python set is a data type, a set concept in mathematics, which corresponds to the set type in the Python language. The difference from list and tuple is that set emphasizes a "membership" and has nothing to do with order, so duplicate elements will be eliminated first.

>>> set([1, 1, 1, 1, 2, 3])
set([1, 2, 3])  #重复元素被排除
>>> set([3, 2, 1])
set([1, 2, 3])  #无序的集合
>>>
Copy after login

Creation of set type

Curly braces are used to create set type variables, which is very similar to a dictionary, except that the value is missing. You will gradually find out later that set Types have some similarities with dictionary keys, such as: unordered, non-repeatable, and must be hashable, so it is natural to use curly braces to express them.

The set type also has a standard representation of set([...]). For example,

>>> {'a','b'}
set(['a', 'b'])
>>>
Copy after login

set adds elements

The set type has built-in The function add is used to add elements to set

>>> A = {'a','b'}
>>> A.add('c')
>>> A
set(['a', 'c', 'b'])
>>>
Copy after login

Related recommendations: "Python Tutorial"

The above is the detailed content of What type is python set?. 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