Tuples in Python

Sequences are a basic data structure in Python that are used to group data together. We learned about Tuple, a type of sequence in Python, in the last article.

Tuples are another type of collections in Python. A tuple is a sequence of objects which is ordered and unchangeable. In this article, we will be learning all about tuples in Python.

Creating Tuples

Tuples are denoted by parentheses in Python. To create a tuple, you just have to write comma-separated values and assign it to a variable.

myTuple = (“Pakistan”, “Berlin”, 1947, 22, 0.5)
print(myTuple)

When the above code is executed, it produces the following result:

(“Pakistan”, “Berlin”, 1947, 22, 0.5)

An empty tuple can be declared by two parentheses with no elements inside. While if you want to declare a tuple with one value, you have to follow it with a comma.

singleValueTuple = (20,)

Indexing and Accessing Values in Tuples

To access values of a tuple, square brackets are used with the index of the value that you are trying to access. Considering the above tuple, we can access its values as:

print(“Second element: “ + myTuple[1])
print(“Fourth element: “ + myTuple[3])

When the above code is executed, it produces the following result:

Berlin
22

Some things to note here:

  • Tuple indices, like tuples, start from 0, not 1.
  • You can also access the tuple elements of a tuple by -1, second last by -2, and so on.

Slicing Tuples

To access a range of values of tuples, we use slicing. It is done by providing the starting and ending index separated by a colon (:) inside the square brackets.

print(“Second to Fourth Element: ” + myTuple[1:3])

When the above code is executed, it produces the following result:

(Berlin, 1947, 22)

Concatenating Two Tuples

If you want to join two tuples, its as simple as adding them using ‘+’ operator in Python.

tuple1 = (56, 34, 78)
tuple2 = (0.5, 0.9, 1.5)
tuple3 = (24, 87, “Russia”) + tuple2

print(“tuple1 + tuple2: ”, tuple1 + tuple2)
print(“tuple3: ”, tuple3)

When the above code is executed, it produces the following result:

tuple1 + tuple2 = (56, 34, 78, 0.5, 0.9, 1.5)
tuple3 = (24, 87, “Russia”, 0.5, 0.9, 1.5)

Updating Tuples

After you have created a tuple, you cannot update its elements or add more as they are unchangeable or immutable. However, there are other ways around it.

If you want to add more values to a tuple, you can do it by concatenating it with another tuple having the needed value and creating a new one.

#myTuple(2) = “Moscow” is invalid

tuple1 = (56, 34, 78)
tuple2 = (99,)
requiredTuple = tuple1 + tuple2

Deleting Tuples and Tuple Values

If you want to delete an entire tuple, you can simply use the del keyword:

del myTuple

But it is not possible to delete elements of a tuple. However, you can obviously use workarounds for that. You can create another tuple with desired values from one tuple and adding your own new values too.

desiredTuple = myTuple[2:4] + (“USA”, 198)
print(desiredTuple)

When the above code is executed, it produces the following result:

(1947, 22, 0.5, “USA”, 198)

This was all about the creation and different operations on tuples. If you have any questions regarding the functions, do ask in the comments below.

Imad

I am a Software Engineer with ample experience in making games, websites, mobile apps and augmented reality solutions.

Pin It on Pinterest