Python Sets | ||||||||||||||||||||||||||||||||||||
![]() ![]() ![]() | ||||||||||||||||||||||||||||||||||||
Sets are used to store multiple items in a single variable. Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage. A set is a collection which is unordered, unchangeable*, and unindexed.
Set Items Set items are unordered, unchangeable, and do not allow duplicate values. Unordered Unordered means that the items in a set do not have a defined order. Set items can appear in a different order every time you use them, and cannot be referred to by index or key. Unchangeable Set items are unchangeable, meaning that we cannot change the items after the set has been created. Duplicates Not Allowed Sets cannot have two items with the same value.
Get the Length of a Set To determine how many items a set has, use the
Set Items - Data Types Set items can be of any data type:
A set can contain different data types:
type() From Python's perspective, sets are defined as objects with the data type 'set':
The set() Constructor It is also possible to use the set() constructor to make a set.
Access Items You cannot access items in a set by referring to an index or a key. But you can loop through the set items using a
Check if "banana" is present in the set:
Add Items To add one item to a set use the
Add Sets To add items from another set into the current set, use the
Add Any Iterable The object in the
Remove Item To remove an item in a set, use the
Loop Items You can loop through the set items by using a
Join Two Sets There are several ways to join two or more sets in Python. You can use the
Keep ONLY the Duplicates The
The
Keep All, But NOT the Duplicates The
The
Set Methods Python has a set of built-in methods that you can use on sets.
Tags: Python Python Sets ![]() ![]() | ||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||
|
SIMILAR POSTSPython TuplesPython DictionariesPython Comparison OperatorsPython Arithmetic OperatorsPrinting string n timesString concatenation by join()Python Conditions and If statementsPython ListsPython String OperationsPython Data TypesPython While Loops/For LoopsPython FunctionsPython VariablesPython CommentsPython SyntaxPython LambdaPython ArraysPython Classes/ObjectsPython Getting StartedPython IntroductionWhat is Python?Python InheritancePython IteratorsPython PolymorphismPython ScopePython ModulesCode Chronicles - A Caffeine-Fueled Journey into Data Software EngineeringMachine Learning Types and Programming LanguagesPython Tutorials for AP Computer Science Principles, Data Projects and High School InternshipMastering Excel Data Manipulation with Python |
OTHER POSTS IN THE SAME CATEGORYPython code to convert from Lunar to SolarPython example to download webpagePython Tutorials for AP Computer Science Principles, Data Projects and High School InternshipPython ModulesPython ScopePython PolymorphismPython IteratorsPython InheritancePython Classes/ObjectsPython ArraysPython LambdaPython FunctionsPython While Loops/For LoopsPython Conditions and If statementsPython DictionariesPython TuplesPython Comparison OperatorsPython Arithmetic OperatorsPrinting string n timesString concatenation by join()Python ListsPython String OperationsPython Data TypesPython VariablesPython CommentsPython SyntaxPython Getting StartedPython IntroductionWhat is Python? |