What is Tuples In Python? a tuple is an ordered, immutable sequence of elements enclosed in parentheses. Tuples are similar to lists, but they cannot be modified once they are created. This means that you can’t add, remove, or replace elements in a tuple.

Here is an example of a tuple:

my_tuple = (1, 2, 3, "hello", True)

In this tuple, there are five elements: the integers 1, 2, and 3, the string “hello”, and the Boolean value True.

What is a tuple?

In Python 3, a tuple is an ordered, immutable sequence of elements enclosed in parentheses. Tuples are similar to lists, but they cannot be modified once they are created. This means that you can’t add, remove, or replace elements in a tuple.

Here’s an example of a tuple in Python 3:

my_tuple = (1, 2, 3, "hello", True)

In this tuple, there are five elements: the integers 1, 2, and 3, the string “hello”, and the Boolean value True.

You can access elements in a tuple using indexing, just like with a list:

print(my_tuple[0])  # Output: 1
print(my_tuple[3])  # Output: "hello"

You can also use slicing to get a portion of the tuple:

print(my_tuple[1:3])  # Output: (2, 3)

Tuples are often used to group related values together, such as a point in two-dimensional space or a date and time. They can also be used as keys in dictionaries because they are immutable.

List in Python

Creating a Tuple

A tuple is a sequence of items separated by commas. These items are enclosed within parentheses (round brackets). Here’s an example of how to create a tuple:

tuple1 = (2, 4, 5, 6, 7, 2)

In this example, tuple1 is a tuple that contains six integers.

Tuples, like lists, can store values of different types. Here’s an example of a tuple that contains integers and strings:

tuple2 = (4, "hi", 6, "Me", 78)

In this example, tuple2 is a tuple that contains five items, including two integers and three strings.

You can also create an empty tuple using a pair of empty parentheses:

empty_tuple = ()

Once you have created a tuple, you can access its elements using indexing, just like with a list:

print(tuple1[0])  # Output: 2
print(tuple2[1])  # Output: "hi"

Tuples are immutable, which means you can’t modify their elements. However, you can create a new tuple that contains some or all of the elements of an existing tuple. This is called tuple unpacking. Here’s an example:

tuple3 = ("apple", "banana", "cherry")
a, b, c = tuple3
print(a)  # Output: "apple"
print(b)  # Output: "banana"
print(c)  # Output: "cherry"

In this example, the three elements of tuple3 are unpacked into the variables a, b, and c.

Accessing data from Tuple

You can access the data stored in a tuple using indexing. In Python, indexing starts at 0, which means the first element of the tuple has an index of 0, the second element has an index of 1, and so on.

Here’s an example of a tuple containing three elements:

my_tuple = ("apple", "banana", "cherry")

To access the first element of the tuple, you can use my_tuple[0]:

print(my_tuple[0])  # Output: "apple"

To access the second element, you can use my_tuple[1]:

print(my_tuple[1])  # Output: "banana"

And to access the third element, you can use my_tuple[2]:

print(my_tuple[2])  # Output: "cherry"

You can also use negative indexing to access elements from the end of the tuple. For example, to access the last element of my_tuple, you can use my_tuple[-1]:

print(my_tuple[-1])  # Output: "cherry"

You can also use slicing to get a subset of the tuple. Slicing returns a new tuple that contains the elements from the specified start index to the specified end index. For example, to get a new tuple that contains the second and third elements of my_tuple, you can use my_tuple[1:3]:

print(my_tuple[1:3])  # Output: ("banana", "cherry")

In this example, the slice starts at index 1 (the second element) and ends at index 3 (one beyond the last element we want to include).

Updating and deleting

Unlike lists, tuples are immutable. Immutable refers to the fact that the items in a tuple cannot be updated or deleted. So, there is no update or deletion of items in tuples. However, it is still possible to perform some operations that may look like modifying a tuple, but actually result in creating a new tuple.

Here’s an example of how to update a tuple in Python:

my_tuple = (1, 2, 3)
new_tuple = my_tuple + (4, 5)
print(new_tuple)  # Output: (1, 2, 3, 4, 5)

In this example, we are creating a new tuple called new_tuple by concatenating the original tuple my_tuple with a new tuple (4, 5). The result is a new tuple with five elements: (1, 2, 3, 4, 5). Note that the original tuple my_tuple remains unchanged.

Similarly, you can delete elements from a tuple by creating a new tuple that excludes the element you want to delete. Here’s an example:

my_tuple = (1, 2, 3, 4, 5)
new_tuple = my_tuple[:2] + my_tuple[3:]
print(new_tuple)  # Output: (1, 2, 4, 5)

In this example, we are creating a new tuple called new_tuple that includes all the elements of my_tuple except for the element with index 2 (the value 3). We do this by concatenating two slices of the original tuple: the slice from the beginning up to index 2 (excluding the element at index 2), and the slice from index 3 to the end. The result is a new tuple with four elements: (1, 2, 4, 5). Note that the original tuple my_tuple remains unchanged.

While these operations may seem like updating or deleting elements from a tuple, they are actually creating new tuples that include or exclude certain elements. This is because tuples are immutable and cannot be modified once they are created.

Summary

A tuple is a sequence of items in Python that are enclosed within parentheses and separated by commas. Tuples are similar to lists but are immutable, meaning that the items in a tuple cannot be updated or deleted. However, it is still possible to perform operations that create new tuples by concatenating, slicing, or otherwise manipulating the original tuple. You can access the data stored in a tuple using indexing or slicing, and you can store values of different types in a tuple. Overall, tuples are a useful data structure in Python that provide a way to store and access data in a convenient and efficient manner.

Write A Comment