[012] Introduction to Python Basics: Tuples and Sets

Koay Yong Cett
7 min readJul 11, 2020
Photo by Chris Bayer on Unsplash

There is another data structure that is known as tuple. A tuples are similar to lists but unlike list we cannot modify the them where they are immutable.

  • This is an example of tuple. Remember that how in a list we could have modified the list. For instance, we try to modified with the my_tuple of index of 1 and equal as z.
  • Here, we see that there is going to have an error and tell us that tuple object does not support item assignment. This means that the tuple is immutable where the tuple cannot be modified.
  • Here, we notice that the item in the tuple can be printed which is similar to list where you can access it through an index.
  • Next, we can use the “in” word to check whether a word is in the tuple or not. Here, true is yielded and means that 3 is exist in the tuple.

We already have lists so why do we have to learn tuple as you know it is pretty similar to list and the only differences is tuple is immutable?

  • Well, we have some benefits of using the tuple. For instance, if we don’t need to change the list and then we will be using the tuples.
  • This is because we can tells other programmers looking at your code where there is tuple and then it should not be changed. This make things more easier and efficient.
  • Tuple makes a code more predictable but it’s less flexible than a list. Then, the tuples is slightly more performance than lists as they usually faster than list. The tuples will be used if you don’t need a list to be changed.
  • We are also able to slice the tuple and get “2,” which start at 1 and end before 2. When we get a single item in tuple, we tend to get a comma(,) after the item.
  • Here, we see that in the result and there is no comma at the end compared to the single item that we get.
  • We may also perform this which is similar to list.

Tuple Methods:

source: https://www.w3schools.com/python/python_ref_tuple.asp
  • There is two types of Python Tuple Methods which is nice and simple.
  • Here, we are able to count the number of times a specified value occur in a tuple by count() method.
  • Then, the index() method will search for that specified value and returns the position where it is found.
  • We can use the built in function len and it tells us that we have 5 numbers in the tuple.

Remember that the tuples are simply lists that are immutable.

Sets:

Sets are simply unordered collections of unique objects and let’s have a look.

  • This is an example of my set.
  • Here, the result returns the unique sets or unique items in a set and there is no duplicates. Everything has to be unique where one number of 5 is added instead of two number of 5.
  • Here, we are only able to add the number of 100 as it is unique. While number of 2 is not added as it is not unique and the original set already contained that data.
  • Here, we are able to form new set from a list and removed all the duplicate values. For instances, the sets will be used in sorting out a list of unique email address and avoid any duplicates. Then, we are not sending multiple same emails to a person over and over.
  • Set object does not support subscriptable or indexing and think of it as a dictionary.
  • This is a way where we would check of something exists in a set.
  • We may also get the length of the set with the len() functions and only the unique objects or items will be counted. Thus, we get 5 in the result instead of 6.
  • We are able to copy the set and clear the set which is shown in the figure above.

Set methods:

source: https://www.w3schools.com/python/python_ref_set.asp
  • Here is the set of method that we will be discussing and talk about. Sets are very useful when comparison is done in between two.
  • The differences is used get a set of differences between two sets which is shown in figure above. This means that what is the difference between my set when compared to your set and there is no 1, 2 and 3 that is viewed from my set.
  • We modifies the set and remove the specified item and then we get the set absence of 5.
  • Here, my_set.difference_update will remove the set in my set that are included in other specified set.
  • The my set is updated with 1,2,3 because we are updating set so that differences are removed. This means that the 4 and 5 are removed.
  • This is different form my_set.difference where the set is not modified and it just shows you the differences.
  • Intersection will return a set of intersection which is the common things that is 4 and 5 between two sets.
  • Shorthand version for intersection.
  • Isdisjoint returns whether two sets have intersection or not or simply determine that the both are not overlapping. If two sets are overlapping, then it is false.Here, the result is false as there is overlapping.
  • While, this shows that the two set is not overlapping which is disjoint and this yielded True.
  • The union returns everything in both sets and remove the duplicates. This return a new set for us.
  • Shorthand version for union.
  • Here, we can see that the my_set is subset of your_set as the 4 and 5 is contained inside your set.
  • Here, the result is False as my set is not a super set as my set does not encompass/include the all the items in your set.
  • While your set is a super set of my set as your set encompass/include all the items in my set.

Thanks for reading my stories 😃

--

--

Koay Yong Cett

A Bachelor CS student with major in Network Security (UniSZa). Every stories I shared is based on my personal opinion. Thanks you. Having my Internship now.