Exploring Python Sets: Versatile Data Structures - w9school
In Python, a set is an unordered collection of unique elements. It is a built-in data type that is used to store multiple items in a single variable. Sets are mutable, meaning you can modify them by adding or removing elements.
Python Sets |
Set
Sets are written with curly brackets.
Set ItemsSet items are unordered, unchangeable, and do not allow duplicate values. UnorderedUnordered 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. UnchangeableSet items are unchangeable, meaning that we cannot change the items after the set has been created. Once a set is created, you cannot change its items, but you can remove items and add new items. Duplicates Not AllowedSets cannot have two items with the same value. ExampleDuplicate values will be ignored:
Example
|
type()From Python's perspective, sets are defined as objects with the data type 'set':
ExampleWhat is the data type of a set?
The set() ConstructorIt is also possible to use the set() constructor to make a set. ExampleUsing the set() constructor to make a set:
|
Python Collections (Arrays)There are four collection data types in the Python programming language:
|
*Set items are unchangeable, but you can remove items and add new items. |
**As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered. |
When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.
Python - Access Set Items |
Access ItemsYou cannot access items in a set by referring to an index or a key. But you can loop through the set items using a ExampleLoop through the set, and print the values:
ExampleCheck if "banana" is present in the set:
Change Items
|
Python - Add Set Items |
Add ItemsOnce a set is created, you cannot change its items, but you can add new items. To add one item to a set use the ExampleAdd an item to a set, using the
Add SetsTo add items from another set into the current set, use the ExampleAdd elements from
Add Any IterableThe object in the ExampleAdd elements of a list to at set:
|
Python - Remove Set Items |
Remove ItemTo remove an item in a set, use the ExampleRemove "banana" by using the
ExampleRemove "banana" by using the
You can also use the The return value of the ExampleRemove a random item by using the
ExampleThe
ExampleThe
|
Python - Loop Sets |
Loop ItemsYou can loop through the set items by using a ExampleLoop through the set, and print the values:
|
Python - Join Sets |
Python - Set Methods |
Set MethodsPython has a set of built-in methods that you can use on sets.
|
Python - Set Exercises |
Now you have learned a lot about sets, and how to use them in Python.
Are you ready for a test?
If Yes, then Click Here
What's Your Reaction?