Featured Post

BUILD A TODO LIST IN PYTHON

Image
Build a TODO List in Python Overview The objective is to build a todo list to which we can add items, remove the added items, edit the items and even if required, delete out the entire list itself. We will learn here step by step, how to build the todo list with the above  CRUD (Create, Remove, Update/edit, and Delete ) features.  This is a CRUD list  app  that we are building as we can Create, Remove, Update items in the list and Delete the entire list itself. We will be using Python3 to build this Project. Importing os and time We import time to let the screen pause for a while before clearing  and we import os to clear the screen . Initializing the list We initialize the todo list with an empty list as there are not items added yet to the list and this is represented in the code aby using an empty square braces   toDoList = [] Defining the print list  The def keyword is used to define a function in python. In the below code, we define the function called printList(). When we define

A Simple Guide to Sets in Python

A Simple Guide To Sets in Python



Photo Credit : Photo by Markus Spiske from Pexels


INTRODUCTION


Sets store multiple data collections in a single variable and the items have no particular order or index and they are also not mutable either, which is why, their items can appear in any order when we perform operations on them, since they are not indexed, so , we can not refer to them by their index position.
A Set is represented by a pair of curly braces. A set does not permit duplicate items.



HOW TO CREATE A SET 


Below, in the code, you can understand, how a set is created. The variable myset is a set containing multiple items, that are string items , namely, 'cat', 'dog' and 'rabbit'. We use the curly braces to enclose the three items as shown below and you can use the print function to get the resultant set containing these items.


  

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.


In the below myset example, this set stores  seven items with the strings, "cat," "dog", "rabbit" and booleans True, False and integers 1 and 5. Here the duplicate items are True and 1 and since they are duplicate items, myset will reject 1 and only display True once in the final output as shown in the code editor 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.


The result will be 6 as there are six items in the last myset set. Since I have already covered the len() function in the blog post of python lists and tuples, there should not be an issue in understanding the concept here either.


  

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 items
myset1 = {"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,booleans
myset= {"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


Since the items in a set is not ordered,  and they are not indexed, we can not access the items inside of a set using the index of the items .


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.


Let us see how this can be done with workable examples as shown below. Again, the resultant set has items which are of a random order compared to the initial set items, since sets are not indexed, as mentioned earlier in this tutorial.



mynewset = {dog", "cat", "rabbit"}


#add items to mynewset

mynewset.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.


































Comments

Popular Posts

Build A Random Quote Machine in React

A Simple Guide to Promises in JavaScript ES6

Welcome to my first Blog Post

How To Fight Programmer's Imposter Syndrome

Top Free Online Websites to Learn Coding

Build A Calculator in React

What Programming Language Should You Learn First As A Beginner?