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

What type is python set?

藏色散人
藏色散人Original
2019-06-29 11:22:0413487browse

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])  #无序的集合
>>>

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'])
>>>

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'])
>>>

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!

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