Introduction
Welcome to my 31st blog post. We have learned about list, tuples datatype, today I learned about another similar datatype which is an unordered collection of well-defined objects. It is called a set. Let's dive into more details.
So let's get started......
What is a set?
It is an unordered collection of data items.
They store multiple items in a single variable.
Set items are separated by commas and enclosed within curly brackets {}.
They are unchangeable, meaning you cannot change items of the set once created. Sets do not contain duplicate items.
Example -
#set does not take repeated values
#Set is a unordered collection of well defined objects. Set does not maintain order
s = {2,4,2,6}
print(s)
{2, 4, 6}
Example -
info = {"Carla", 19, False, 5.9, 19}
print(info)
{False, 19, 5.9, 'Carla'}
In the above example, we see that the items of the set occur in random order and hence they cannot be accessed using index numbers. Sets do not allow duplicate values.
Empty set
Below is the code to create an Empty set
#create empty set
set1 = {} #the way to make dict and set is same. So here type is dict
print(type(set1))
#to create empty set use below syntax
set2 = set()
print(type(set2))
<class 'dict'>
<class 'set'>
Accessing set Items
Using a For loop - You can access items of set using a for loop.
info = {"Carla", 19, False, 5.9}
for item in info:
print(item)
Output -
False
Carla
19
5.9
Note: all the operations similar to list can be performed on sets except that repetition of values is not allowed.
Resources Used
You can watch the video of Day#31 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day #31 we learned about sets and its various properties in python. We also learnt about how set stores unique items.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding. This is it for Day #31
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.