PYTHON PROGRAMMING CLASS (THE BASICS) || DAY 3 - PYTHON SETS COLLECTION | WEEK 2
Good day everyone!
Hope you're doing great!! Today is the third day of the Python Programming class week 2. If you have followed my week 1 till the end, you must have acquired some basic Python programming skills that can help you start your programming career. However, if you did not follow my week 1 classes, please visit by blog now @anyiglobal to see all my week 1 classes.
I brought out this idea of teaching python on steemit because of the need for python programming skills in the world of technology today. This is the new trend in tech now because python can be used for various purposes in many applications ranging from web development, automation, artificial intelligence, software testing to many others. I decided to start this programming class on steemit communities because the communities comprise mainly of people who are still eager to learn new skills. Developers and non-developers alike can use this python.
Note: The English language used in this class is literally understandable by a layman. So let's begin...
Sets is a type of collection that is unordered and unindexed. Unordered means that the items in a Sets collection is printed in a random manner. Sets collections are enclosed in a curly braces "{}".
For example, if we have a set of names;
names = {"Anyiglobal", "Reminiscence", "Swaylee", "Wealthmary"}.
Then the output may appear in an unordered manner like this;
names = {"Reminiscence", "Swaylee", "Anyiglobal", "Wealthmary"}.
Sets are unindexed because we cannot access items in it using an index number enclosed in a square bracket "[]" like this "names[3]". Sets collection does not allow duplicate members unlike Lists and Tuples that allows duplicate members of the items.
We will detail tuple collection in this class by discussing the following:
- How to access set items.
- How to change values in a set.
- Checking the length of a set.
- Adding items to the set.
- Removing items from the set.
- Using the set() constructor to create a set.
Note that Sets can contain collection of the same or different data types. It can collect string data types, and it can collect numeric data types.
EXAMPLE OF SETS COLLECTION
Let's consider a set of names;
For Example;
names = {"Anyiglobal", "Reminiscence", "Swaylee", "Wealthmary"}
print(names)
Output
Python Sets Collection
From the screenshot above, we can see that the output appeared in an unordered manner i.e. randomly.
NB: Copy the code above and try it out in your text editor.
ACCESS ITEM IN A SET
As we said earlier, items in a set cannot be accessed by referring to an index. This is because Sets collection is unordered. However, we can access items stored in the set by using "for loop" or checking if a specific value is present in the set by using the "in" keyword.
For Example,
names = {"Anyiglobal", "Reminiscence", "Swaylee", "Wealthmary"}
for x in names:
print(x)
Output
Swaylee
Reminiscence
Anyiglobal
Wealthmary
Accessing item in a Set
From the code above, we can see how we used "for loop" to access all the items in the set. We can as well access a specific value by checking if the value is present in the Set.
For Example;
Let's check if the name "Reminiscence" is present in the Set:
names = {"Anyiglobal", "Reminiscence", "Swaylee", "Wealthmary"}
print("Reminiscence" in names)
Output
True
Checking if a specific value is present in a set
From the code above, we can see that the code returned True because the item "Reminiscence" is present in the set.
NB: Copy the code and try it out in your text editor.
CHANGING ITEMS IN THE SET
We cannot change items in a set because it is immutable i.e. unchangeable.
ADDING ITEMS TO THE SET
To add items to the set, we can use add() and update() methods to achieve that. To add one item to a set, use "add()" method, However, to add more than one item to a set, use "update()" method.
For Example,
Let's add a new name to the sets collection. Let's add "Steemcurator01" to the set:
names = {"Anyiglobal", "Reminiscence", "Swaylee", "Wealthmary"}
names.add("Steemcurator01")
print(names)
Output
{'Reminiscence', 'Steemcurator01', 'Anyiglobal', 'Wealthmary', 'Swaylee'}
Adding item to a Set
From the screenshot above, we added "Steemcurator01" to the set as we can see in the screenshot above.
To add multiple items to a set, we use the "update()" method.
For Example,
Let's add multiple items to the set.
names = {"Anyiglobal", "Reminiscence", "Swaylee", "Wealthmary"}
names.update(["Victoh98", "Starrchris", "Solexybaba"])
print(names)
Output
{'Wealthmary', 'Reminiscence', 'Solexybaba', 'Victoh98', 'Swaylee', 'Starrchris', 'Anyiglobal'}
Adding multiple items using the Update method
From the screenshot above, we can see how we use the "Update()" method to add multiple names to the set.
NB: Copy the code and try it out in your text editor.
CHECKING THE LENGTH OF A SET
To check the length of values in a set we use the "len()" method.
For Example,
names = {"Anyiglobal", "Reminiscence", "Swaylee", "Wealthmary"}
print(len(names))
Output
4
From the codes in the screenshot above, we can see that the code returned 4 as the length of the sets collection.
REMOVING ITEM FROM A SET
To remove an item from a set, we use the "remove()" or "discard"() methods.
For Example,
Let's remove "wealthmary" from the set using the "remove()" method.
names = {"Anyiglobal", "Reminiscence", "Swaylee", "Wealthmary"}
names.remove("Wealthmary")
print(names)
Output
{'Reminiscence', 'Swaylee', 'Anyiglobal'}
Removing item from a set using the remove() method
Looking at the screenshot above, we can see that we removed "Wealthmary" from the set.
Let's also use the "discard()" method to remove "Wealthmary" from the set.
names = {"Anyiglobal", "Reminiscence", "Swaylee", "Wealthmary"}
names.discard("Wealthmary")
print(names)
Output
{'Reminiscence', 'Anyiglobal', 'Swaylee'}
Removing item from a set using the discard() method
From the code in the example above, we used the "discard()" method to remove "Wealthmary" from the set.
NB: Copy the code and try it out in your the text editor. We can also "pop()" method to remove the last item in a set. In the same way, we can also use "clear()" method to clear the items in a set. And we can also use the "del" keyword to delete the set entirely.
NB: Please refer to the class on Lists collection and Tuples collection to see how clear and delete a set using the "clear()" method and "del" keyword.
PYTHON SET() CONSTRUCTOR
We can use set() constructor to make a set collection.
For Example,
names = set(("Anyiglobal", "Reminiscence", "Swaylee", "Wealthmary"))
print(names)
Output
{'Wealthmary', 'Anyiglobal', 'Reminiscence', 'Swaylee'}
Python set() constructor
From the code in the screenshot above, we can see that the set() constructor was used to make the collection a set.
NB: Copy the code above and try it out in your text editor.
Conclusion
Python sets collection is very important in Python programming language. As a programmer you need to have a knowledge of collections in Python. In this class, we discussed how to access items in a set, how to add items to a set, how to change set value, changing items in a set, checking the length of a set, removing item from a set, and how to make a set collection using set() constructor. Thank you for being part of this class, I hope you learnt something new today!!
This is the end of this particular class. I believe that if you followed this class till the end, you must have grabbed one or more information from the class. Please make sure to practice along with your laptop and text editor to grab every bit of the information passed.
Please do well to follow this blog, and also don't forget to resteem this post so that it can reach larger number of steemians who wants to learn this skill.
I am @anyiglobal, a Computer Scientist, Software Engineer and a Blogger!
Cc:
@steem-database, @ponpase, @kouba01, @pelon53, @reminiscence01, @steemchiller, @justyy
Thank you for contributing to #LearnWithSteem theme. This post has been upvoted by @ripon0630 using @steemcurator09 account. We encourage you to keep publishing quality and original content in the Steemit ecosystem to earn support for your content.
Regards,
Team #Sevengers
The #learnwithsteem tag focuses on teaching through tutorials and lessons some knowledge, skill or profession that you have. Please avoid using it if it's not about that. Thank you!
Your post is manually rewarded by the @nftmc Community Curation Trail.
Join the NFTMC community to get rewarded.
USE TAG - #nftmc
Curation Trail- @nftmc
Discord- https://discord.gg/5P57gwYYcT
Twitter- https://mobile.twitter.com/NFTMC3
Congratulations !!!
You got upvote from the Steemit Travel community.
Join the Steemit Travel community. We are here for you travelers.
We are also collaborating with @steem-database who has reached with 12 K SP to increase the value of your posts.
Share your travel stories with us.
Steemit Travel
https://steemit.com/trending/hive-163291
DELEGATION
We are very open to receiving delegations from anyone who wants to support the community. if you are interested in becoming a delegator for Steem-Database you can give any delegation you like :
Ok thanks for the collaboration
Penalized for spam tagging of other accounts.
Please I don't understand! I would be glad if you explain further!! I already have friends on steemit platform, so how does tagging my friends and Community admins and moderators become a crime now? This is social media like Facebook and others, so why is tagging here different from other social media platforms?
Please I need an urgent reply to this, as I have never read any guidelines regarding to tagging steemit friends and community admins as offense before! Please if this is a new guideline I demand to know!!
Greetings my friend.
Tagging others is a strategy that can be considered abuse, since you would be advantaging others in terms of drawing the attention of important accounts to receive votes, no matter how good a relationship you have with other users, it should be neutral how you give publicity to their content,
For this, as I have informed you before, the correct way to promote your content is to send 0.001 Steem to users inviting them to check your link.
If everyone would apply your strategy, for sure, all tagged users would see their notifications crowded.
I hope you understand.
I understand bro. But that wouldn't warrant a penalty on my merited post that is selected among the best to receive upvote. While I am penalized unjustly, another user by name abialfatih committed the same tagging as I did but he was not penalized by SC01... now what can you call this? Is it not partiality and favouritism? Reason this very well as human being and give me feedback! It's quite unfair to me.
Another steemit user by name "abialfatih" did the tagging in his post but you didn't penalize him for that, you gave him his full vote percentage... Then why did you deny me of my full vote percentage as I was selected among the best articles as abialfaith?
This is absolutely partiality on this Blockchain! My efforts of creating such quality post you rendered in vain by denying me my full voting percentage!
Please I request for my full vote to be given to me as soon as possible if the other defaulter is not penalized immediately. Take a look at the screenshot below:
@reminiscence01
The user "abialfatih " tagged only the team of the community where he is posting, instead in his case, you are tagging witnesses, and members who have nothing to do with the community where you posted.
Votes should not be demanded, the support for the publications is free term by those responsible.
There's no guideline that said we should only tag community admins and moderators! If there's any guideline for that, please show me I want to see. The said witness as according to you, is my friend and isn't complaining of me tagging him in my post. And the said witness is always responding to my post anytime I tag him because he value my quality programming contents.
Don't justify this whole thing by what is not a guideline on this platform. Please rephrase what you just said here because there's no proof for that! I can only believe what you just told me here by showing me some proof that a user can only tag community members!
Apparently, the people I tagged are not complaining, then why the Penalty, I demand to know immediately!
Here you have this guide my friend.
https://steemit.com/faq.html#What_is_considered_spam_or_abuse
Thanks for sending this across! But I can't still find where tagging steemit friends is an abuse!
Thanks for your concern in this issue! I appreciate your effort, but your thesis is lopsided, you're not talking in the angle of the other steemit user that defaulted too but was not penalized.