A set is another container in Python just like lists and tuples (which I have covered in my last two articles). If you haven’t already learnt about the first two container in Python, go learn about those two containers right now or after reading this post. Since, these are not inter-related, when you learn about those containers is totally upto you, but do check them out because they are EXTREMELY IMPORTANT for you to move ahaead and learn advanced coding in Python.

Lists:

https://noobcodepro.blogspot.com/2020/10/lists-in-python.html

Tuple:

https://noobcodepro.blogspot.com/2020/11/tuples-in-python.html

 OK so let’s learn about your third container SETS.

What is a Set in Python?

Set can be defined as a group of unique elements. (Seems simple right?? Well, because it is !!! Learning to code is simple, IF you do it the right way).

Let’s understand it better with an example:

And when I print this set, the output will be:

Wondering why it didn’t print the second ‘5’, because like i said earlier, it is a set of UNIQUE ELEMENTS, it doesn’t consider elements that repeat themselves. (Guess not so simple, huh?)

Like lists and tuples, sets need to be declared with a certain set of brackets (Well, there is only one bracket left anyway), CURLY BRACES {}. So, Curly braces{} are used to define a set.

(Please forgive my shitty drawing, I am doing this on a laptop LOL...)

Another important thing about sets is that Sets never follow a specific sequence. What I mean is, you could refer to a specific element in a list or a tuple by using its index value, but in sets you cannot use index value BECAUSE ELEMENTS IN A SET DON’T HAVE ANY. Whenever you print a set it prints the elements in a random order, thus not following a specific sequence.

So, if we print set, ‘my_set’:

 It will give the following output:

See, how ‘36’ and ‘88’ changed it’s place randomly??

Like I said above, Set does not support indexing because elements in a set are NOT sorted in a specific order.

 

 

Yeah, I know this is a really small article, but this is all you need to know to have a brief understanding about Sets in Python.


If you are a beginner, intermediate, advanced or just someone interested in programming, feel free to join our telegram channel and be among people like you:


And do you know the best part? Joining it is FREE !!!

So go ahead click on the link and I will see you there. 

HOPE YOU HAVE AN AWESOME DAY AHEAD !!!