GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Python Sets - GeeksforGeeks Python SetsLast Updated : 21 Oct, 2026 Set is a built-in Python data type used to store a collection of unique items.
Example: This example shows how to create a set with some numbers and check its type.
Output {10, 50, 20}
<class 'set'>
Type Castingset() method is used to convert other data types, such as lists or tuples, into sets.
Output {'b', 'a', 'c'}
Check unique and ImmutableSets cannot have duplicate values. While you cannot modify the individual elements directly, you can still add or remove elements from the set.
Output
Explanation:
Heterogeneous ElementSets can store heterogeneous elements in it, i.e., a set can store a mixture of string, integer, boolean, etc datatypes.
Output {True, 52.7, 'Geeks', 10, 'for'}
Frozen SetsFrozenset is an immutable version of a set. Its elements cannot be changed after creation, but you can perform operations like union, intersection and difference. Use frozenset() to create one.
Output ('Normal Set:', set(['a', 'c', 'b']))
('Frozen Set:', frozenset(['e', 'g', 'f']))
Internal working of SetPython sets use a hash table to store unique elements and perform insertion, deletion, and lookup efficiently. Python calculates a hash value to determine each element's initial position in the table.
Methods for Sets1. Adding elements: add() function is used to insert new elements into a set. It automatically ignores duplicates.
Output {'c', 'd', 'a', 'b'}
2. Union: union() function combines two sets and returns a new set with all unique elements.
Output {'z', 'y', 'x'}
3. Intersection: intersection() function returns a new set containing elements that are common to both sets.
Output {2, 3}
4. Difference: difference() function returns a set containing elements that are in the first set but not in the second.
Output {1}
5. Clear: clear() function removes all elements from a set, leaving it empty.
Output set() Operators for SetsSets and frozen sets support the following operators:
Comment Explore | ||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |