The world’s leading publication for data science, AI, and ML professionals.

4 Basic Commands When Working with Python Tuples

Making you understand the characteristics of Python Tuples and how you deal with them

Photo by Washington Oliveira 🇧🇷 on Unsplash
Photo by Washington Oliveira 🇧🇷 on Unsplash

The Python tuple is used to store multiple values in a variable. It is one of four data structures that are pre-installed in Python. In addition to the tuple, these also include the dictionary, the set, and the list.

The tuple is ordered and cannot be changed. On the one hand, this means that the elements have a specific and constant order. Due to the fixed order, the tuple also allows duplicates. On the other hand, the tuple cannot be changed after it has been defined. Thus, no elements can be deleted or added.

Why is it so important to know the different Data Types?

Python has a total of four different data types, which are already present in the basic installation and can therefore not be used only by installing a module, such as Panda’s DataFrames. These can be used for a wide variety of use cases and also form the basis for many other data types used in modules.

The four basic data types in Python are:

  • The list is an ordered collection of elements, which is changeable and can also contain duplicate elements.
  • The tuple is in effect a list, with the difference that it is no longer changeable. So no elements can be added or removed afterward.
  • The set does not allow duplicate entries. At the same time, the arrangement of the elements within the set is variable. The set itself can be changed, but the individual elements cannot be changed afterward.
  • Since Python version 3.7, a dictionary is an ordered collection of elements that can be changed. In the earlier versions, the dictionary is unordered.

1. Define a Tuple

We can create a Python Tuple by defining the elements in round brackets and separating them with commas. Elements with different data types can be stored in a tuple without any problems.

2. Query Elements

Due to the order of a Tuple, we can resort to indices to retrieve individual elements from the tuple. It should be noted that the counting of elements starts at 0. If we want to retrieve a value from the end, on the other hand, we start counting at 1.

If we don’t know the index of a certain element yet, we can ask for it with the method "index". Since the order within the Python Tuple does not change, this value also remains.

3. Change Elements

As we have already learned, Tuples are actually immutable. That is, once we have defined a Tuple, it is no longer possible to add or delete elements.

To be able to change Tuples anyway, we use a little trick. We first convert the Tuple into a Python list. Since this is changeable, we can simply add or remove elements here. Then we convert the list back to a Tuple. This way we have indirectly changed the elements of the Python Tuple.

4. Merge Tuples

If we want to merge two or more Tuples, we can simply use the "+" operator. The first named Tuple is accordingly in the order before the second named Tuple.

This is what you should take with you

  • The Python Tuple is one of four pre-installed data structures in Python.
  • It is used to store multiple values in a single variable.
  • The Tuple cannot be modified after it has been created. It is also ordered, which means that the values have a predefined order.

_If you like my work, please subscribe here or check out my website Data Basecamp! Also, medium permits you to read 3 articles per month for free. If you wish to have unlimited access to my articles and thousands of great articles, don’t hesitate to get a membership for $5 per month by clicking my referral link:_ https://medium.com/@niklas_lang/membership


An Introduction to TensorFlow

Why do we use XML in Data Science?

Redis: In-Memory Data Store easily explained


Related Articles