Featured Post
A Simple Guide to Sets in Python
- Get link
- X
- Other Apps
A Simple Guide To Sets in Python
INTRODUCTION
HOW TO CREATE A SET
mytuple = {cat","dog","rabbit"}
print(myset)
DUPLICATE ITEMS NOT ALLOWED IN SETS
Sets do not permit duplicate items and this is demonstrated in the code below. Also, note that in a set, the boolean True and the integer 1 is considered as one and same item and either of them when added more than once, will be treated as a duplicate item and 1 will be displaced by the boolean True. In other words, if there is and True present in a set, then, the resultant set will only display and store True and not the number 1. Let us understand this with the help of an example code as shown below.
myset= {cat","dog","rabbit", True, False, 1,5}
print(myset)
#result#myset = { False, True, 5, "dog", "cat", "rabbit" }
HOW TO DETERMINE THE LENGTH OF A SET
The length of a set could be determined by using the len() function as in the case of lists and tuples in Python. Let us check in the code editor below. It is the same way as in the case of lists and tuples, so, you will not need much explanation for the below code.
myset= {cat","dog","rabbit", True, False, 1,5}
print(myset)
#result#myset = { False, True, 5, "dog", "cat", "rabbit" }
print(len(myset))
#6
WHAT ARE THE TYPE OF ITEMS THAT SETS CAN STORE
Sets can store strings, integers and booleans. Let us consider various sets with different examples of the various scenarios as shown below in the code editor.
#storing string itemsmyset1 = {"cat","dog","rabbit"}print(myset1)# result{"cat","dog","rabbit"}
#storing integers
myset2 = {1,2,3,4,5,6,7,8,9}print(myset2)# result{1,2,3,4,5,6,7,8,9}
#storing booleans
myset3 = {True, False, True, False, False}print(myset3)#result{False, True}
#storing mix items of strings,integers,booleansmyset= {"cat", "dog", "rabbit", True, False, 1, 5 }
HOW TO MAKE A NEW SET WITH A SET() CONSTRUCTOR
Using the set() constructor, we can make a new set as demonstrated in the code below. Since a set is always without an order, the below result of the mynewset which is newly constructed using the set() constructor, will be showing a random order of its resultant items as shown below.
mynewset = set(("dog", "cat", "rabbit"))print(mynewset)
#result will be a set as shown below{"cat", "rabbit, "dog}
CAN WE ACCESS THE ITEMS IN A SET
MUTABILITY OF ITEMS INSIDE A SET
Since the items in a set are Not changeable, but, we can add, remove items inside a set. We can add items to a set using the add() function and remove items from a set using the remove() function. But, we can not change or replace any items existing in a set as they are not changeable at all.
mynewset = {dog", "cat", "rabbit"}#add items to mynewsetmynewset.add("mouse")print(mynewset)#result{"cat", "mouse", "rabbit", "dog"}
CONCLUSION
Sets are in-built data structures storing collection of items in a single variable and they are represented by curly braces. Sets are immutable as they are not indexed by structure and any operations executed on them, result in random order of the items in the resultant set. We can not change the value of any item inside a set, however, we can add or remove items in the set by using the add() and remove() functions respectively. Sets are not ordered and non-indexed, due to which, we can not access items in a set using their index .
Hence, we can not change or replace any items inside a set and as mentioned above, we can only add or remove items to a set. Sets can store strings, booleans and integers or a mix of all these data types. We can identify the length of a set by using the len() function. Sets can not store duplicate items. We can create a new set using the set() constructor as explained in the above paragraphs with code examples.
- Get link
- X
- Other Apps
Comments
Post a Comment