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:

  1. iterable (optional): objects can be string, tuple, set, list, dictionary. .or iterator object .

The value returned from set

  1. If no parameters are passed, set () creates an empty set.
  2. 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

May be interested

  • Help () function in PythonHelp () function in Python
    the built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
  • Sum () function in PythonSum () function in Python
    the built-in function sum () in python returns the sum of all numbers in iterable.
  • The slice () function in PythonThe slice () function in Python
    the slice () function in python returns a slice object that helps you determine how to cut an existing string.
  • The chr () function in PythonThe chr () function in Python
    in python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
  • The iter () function in PythonThe iter () function in Python
    continuing 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.
  • The pow () function in PythonThe pow () function in Python
    the pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
  • The reversed () function in PythonThe reversed () function in Python
    the reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
  • Exec () function in PythonExec () function in Python
    the exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.
  • Built-in Python functionsBuilt-in Python functions
    in the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.
  • The compile () function in PythonThe compile () function in Python
    the compile () function returns a code object in python from the specified source. so how to use compile () function? please find out in this article.