Learning Python From Scratch: Data Structures — Tuples

Elif Çelik
CodeX

--

Hello everyone! I am one of the Sisterslab’s Women in Tech Academy project participants, supported by Toplum Gönüllüleri Vakfı. The project aims to empower 25 women between the ages of 20–28 with software training for 3 months and seek their participation in the workforce in the sector. You can find more information about the project at this link: https://sisterslab.co/women-in-tech-academy/

I will try to cover python from scratch in this medium article series. Today we are going to learn another data structure, tuples. You can check the other articles from my medium account!

TUPLES

Tuples are a data structure that is very similar to lists. The aspects that distinguish them from lists are that their contents can not change. Hence, tuples are immutable. Let's have a look at the properties of tuples.

  • Tuples are ordered and can be accessed by index
  • Tuples cannot be modified, after which items cannot be added or deleted
  • Tuples are defined in parentheses (), and all of the elements are separated by commas.
  • Tuples can store different types of data. (String, Integer, Float, List, etc.)
  • They can have two items with the same values, so there can be duplicate data.

How To Create Tuples

The creation of tuples is really easy. All you need to do is give a tuple name and define it with parentheses. Here is some example for creating tuples:

As I mentioned before you can create tuples with integers, and strings or you can even add a list into a tuple.

Looping Through a Tuple

You can also loop through the elements of a tuple as we do in lists. For example, you can print the items in a tuple by writing for a loop.

I also stated that we can access items with indexes, as in lists. If you want to reach certain indexes in the tuple, you can do this with a tuple-length for loop:

Accessing Items in Tuple

You can access certain items in the tuple in various ways. As used in lists, you can obtain elements in the appropriate range by specifying [start: end] after a certain item. Or you can print the tuple starting from the end.

Finding Values Index in a Tuple

If you want to find the index of an item in a tuple, you can find the index using tuple.index().

You can also check if a certain element is in the tuple like the example below.

Changing Tuple’s Type-Adding Elements

As I mentioned above, the tuple is immutable, but you can try adding and removing elements from the tuple using different methods. Let’s take the following code as an example, since we first created our data structure as a tuple, it will give the class type as a tuple in the first output. In the next part, since we convert our tuple to a list and print its type, the second print will output the list. Since we are converting it to a new list, we can easily add elements. Finally, we convert the list to which we added a new element to a tuple. Thus, we add a new element to the tuple.

In other words, if you want to add and remove operations in a tuple, you can first convert it to a list and do the operations you want, and then convert it back to a tuple.

Copying a Tuple

If you want to copy a tuple, you can create a tuple with the same items as I used below.

Merging Tuples

It’s also very easy to concatenate multiple tuples, you can use the plus operator for that. As the example below, we have a new tuple named tuple3 containing tuple1 and tuple2 items.

So this was all about the Tuples! Hope you enjoyed it and learned new things. You can also check out my other articles about Python from here.

Also, don’t forget to follow me and my other social media accounts.

You can find all the resources I used in my article here!

Sources:

Geeks For Geeks Python Data Structures

Educative Python From Scratch

--

--

Elif Çelik
CodeX
Writer for

A Computer Engineer who is interested in Machine Learning and Deep Learning.