Menu Close

Python frozenset Function

Python provides a built-in function frozenset, that is used to create the unchangeable set. In this article, we will learn about the Python frozenset function along with some examples.

In the previous tutorial, we have seen the Python float function to convert any number to a float number.

Python frozenset() Function

Python frozenset function is a built-in function that is used to create and return unchangeable frozenset object ( Like set object ).

Syntax

The Syntax of frozenset function in Python is:-

frozenset(iterable)

Parameter

Python frozenset method accepts one parameter:-

  • iterable:- An iterable object like list, set, tuple, etc.

Return Value

frozenset function in python unchangeable object.

Python frozenset examples

Here we will understand python frozenset function along with examples.

Example 1:

mylist = ['Python', 'Java', 'C++', 'PHP', 'HTML']
result = frozenset(mylist)
print(result)

Output

frozenset({'Java', 'HTML', 'C++', 'PHP', 'Python'})

What happen, If you try to change your frozenset item.

Example 2:

mylist = ['Python', 'Java', 'C++', 'PHP', 'HTML']
result = frozenset(mylist)
result[1] = 'CSS'
print(result)

Output

Traceback (most recent call last):
  File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\test.py", line 3, in <module>
    result[1] = 'CSS'
TypeError: 'frozenset' object does not support item assignment

When you try to change your frozenset item, you will get an error.

Conclusion

In this tutorial, you have learned all about the Python frozenset method to create an unchangeable object. You can use any iterable ( list, set, tuple ) to convert frozenset object.

If this article helped you, please keep visiting for further python built-in functions tutorials.

Other Python built-in functions


For more information:- Click Here

Previous Article
Next Article

Related Posts

Leave a Reply