The function set () in Python
The set () function built into Python is used to create a set object from the given iterable.
In this article, TipsMake.com will learn with you about set (), syntax, parameters and specific examples. Invites you to read the track.
Syntax of set () function in Python
set([iterable])
Parameters of the set () function
Python's set () constructor has a single parameter:
iterable
(optional): objects can be string, tuple, set, list, dictionary. .or iterator object .
The value returned from set
- If no parameters are passed, set () creates an empty set.
- If iterable is passed as a parameter, it will create a set of elements in iterable.
Example 1: Create a collection from string, tuple, list, range
# tập hợp rỗng print(set()) # string print(set('Python')) # tuple # viết bởi TipsMake.com print(set(('a', 'e', 'i', 'o', 'u'))) # list print(set(['a', 'e', 'i', 'o', 'u'])) # range print(set(range(5)))
Running the program, the result is returned:
set() {'P', 'o', 't', 'n', 'y', 'h'} {'a', 'o', 'e', 'u', 'i'} {'a', 'o', 'e', 'u', 'i'} {0, 1, 2, 3, 4}
Example 2: Create a collection from set, dictionary and frozen set
# set print(set({'a', 'e', 'i', 'o', 'u'})) # dictionary # viết bởi TipsMake.com print(set({'a':1, 'e': 2, 'i':3, 'o':4, 'u':5})) # frozen set frozenSet = frozenset(('a', 'e', 'i', 'o', 'u')) print(set(frozenSet))
Running the program results in:
{'a', 'o', 'i', 'e', 'u'} {'a', 'o', 'i', 'e', 'u'} {'a', 'o', 'e', 'u', 'i'}
Example 3: Creating a collection from an iterator object
class PrintNumber: def __init__(self, max): self.max = max def __iter__(self): self.num = 0 return self def __next__(self): if(self.num >= self.max): raise StopIteration self.num += 1 return self.num printNum = PrintNumber(5) # tạo set # viết bởi TipsMake.com print(set(printNum))
Running the program results in:
{1, 2, 3, 4, 5}
See also: Python built-in functions
5 ★ | 1 Vote
You should read it
May be interested
- Function open () in Pythonthe open () function is built into python to use to open a file and return the corresponding file object. follow the article to learn more about the syntax, parameters and usage of open ()
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- Max () function in Pythonpython's built-in max () function returns the largest element in an iterable or the largest of the passed parameters
- Min () function in Pythonthe built-in min () function in python returns the smallest element in an iterable or smallest of passed parameters. so, how does the syntax of min () function work, what parameters and how does it work?
- Format () function in Pythonthe format () function is built into python to use to format an input value into a specific format.
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...