Set in Python
Now, we are going to learn and explore a very interesting and useful concept in python, which is set. Basically, just like the list, or tuple, the set is one of the datatypes, also used to store a collection of data. The thing is that set is an unordered collection of elements, in which, duplicate elements are not allowed. The thing is that set is unordered, and unindexed, and also, duplicate elements are not allowed in the set. At times, there are situations, when we need to make use of sets in our programs, and now, we are going to explore much about the concept of sets in python. So, Let’s dive in now.
Set in Python
Well, first of all, Let’s recall how we have defined the set. Set is an unordered, unindexed collection of data, where duplicates are not allowed. Also, the thing is that in the set, we can have only immutable objects, which means that in the set, we cannot have a list, dictionary, etc. but we can have int, float, string, etc. Also, the thing is that the set itself is mutable, which means that we can add or remove items from the set.
Soon, we are going to deal with many different things related to a set, but now, Let’s explore the things one by one. Here is an insight into what are we going to cover here –
- How to create a set?
- Getting the length of the set
- Accessing the set elements
- Set methods
- The add method
- The clear method
- The copy method
- The difference method
- The intersection method
- The union method
- The remove method
- The isdisjoint method
- The issubset method
- The discard method
- The difference_update method
- The pop method
- The issuperset method
- The update method
- The symmetric_difference method
- The symmetric_difference_update method
- The intersection_update method