How to Use set() Function in PythonPython Basics


The set() function creates a set object which will appear in random order.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-


x = set('ABCDE')
print(x)

x = set('AAAABCDE')   # Duplicate items will be deleted
print(x)

x = set('AAABBBCCCDDDEEE') # Duplicate items will be deleted
print(x)

Output:

{'E', 'D', 'B', 'A', 'C'}
{'E', 'D', 'B', 'A', 'C'}
{'E', 'D', 'B', 'A', 'C'}

Syntax

set(obejct)

Parameters

Name Description
object A sequence, collection, or an iterator object.

Return Value

It returns a set object.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us