Menu Close

Python set() function

In this Python built-in tutorial, we are going to learn all about Python set functions along with examples.
In previous tutorials, we have seen the Python round function to find the rounded version of a specified value with a specified number of the decimal.

Python set() function

Python set function is a built-in function that is used to create the set object. In Python, the set is unordered, b, and the set does not allow duplicate items.

Read more about Python list

Syntax

The syntax of set function in Python is:-

set(iterable)

Parameter

set function in Python support iterable

Return Value

Return value of Python set function is set.

Python set function example

Here we will take an example to understand Python set function.

Example:

result = set((1,2,3,4,5,6,7,8,9,10))
print(result)
x = set(('Python', 'Java', 'C++', 'PHP'))
print(x)

Output

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{'C++', 'Python', 'Java', 'PHP'}

Use for loop

You can use Python for loop with set function in Python.

#use for loop to iterate over set object
for i in set(('Python', 'Java', 'C++', 'PHP')):
	print(i)

Output

PHP
Java
Python
C++

Conclusion

In this article, you have learned all about the Python set function to create the set and perform a set operation on created object.
I hope this article will help you, if you like this article, please share and keep visiting for further Python tutorials.

Python built-in functions


For more information:- Click Here

Previous Article
Next Article

Related Posts