Table of Contents
This updated guide examines The Function Set ()() in Python and organizes the essential facts, background, and practical takeaways in clear American English.
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
FAQ
What is The Function Set ()() in Python about?
It provides a structured overview of function set (), explains the main context, and highlights practical takeaways for readers.
Why does this topic matter?
Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.
How should readers use this information?
Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.
Reader Comments 0
Sign in with email or Google to join the discussion.