Home > Article > Backend Development > Detailed explanation of set() function in Python
The set() function creates an unordered set of non-repeating elements, which can perform relationship testing, delete duplicate data, and calculate intersection, difference, union, etc. Hope it helps everyone.
Syntax
set Syntax:
class set([iterable])
Parameter description:
iterable – iterable object object;
Return value
Return the new collection object.
Example
The following example shows how to use set:
x = set('runoob') y = set('google')print(x)print(y) a = x & y # 交集print(a) b = x | y # 并集print(b) c = x - y # 差集print(c)
Output:
{'n', 'r', 'u', 'b', 'o'}{'l', 'e', 'g', 'o'}{'o'}{'g', 'r', 'e', 'b', 'l', 'n', 'u', 'o'}{'n', 'r', 'u', 'b'}
Related recommendations:
Introduction to the use of set() class
The above is the detailed content of Detailed explanation of set() function in Python. For more information, please follow other related articles on the PHP Chinese website!