Home » Python set() function with Examples

Python set() function with Examples

by Online Tutorials Library

Python set() Function

In python, a set is a built-in class, and this function is a constructor of this class. It is used to create a new set using elements passed during the call. It takes iterable as an argument and returns a new set object. The constructor syntax is given below.

Signature

Parameters

iterable: a collection of immutable elements.

Return

It returns a new set.

Let’s see some examples of set() function to understand it’s functionality.

Python set() Function Example 1

A simple example to create a set of using iterable elements.

Output:

set()  {'1', '2'}  {'a', 'n', 'v', 't', 'j', 'p', 'i', 'o'}  

Python set() Function Example 2

Output:

{'15', '13', '12'}  {'n', 'v', 'a', 'j', 'p', 't', 'o', 'i'}  {1, 2, 3}  

Python set() Function Example 3

Here, we are creating a set of filtered elements. The geteven function returns even values.

Output:

{8, 2, 4, 6}  

Next TopicPython Functions

You may also like