Python 3 – Tuples
In Python programming language, a tuple is a collection of objects that are ordered and unchangeable. Unlike other collection data types like lists, tuples cannot be modified after they are created. This means that once a tuple is created, it remains in that state for the rest of the program. This article aims to provide an overview of tuples and explain how to use them in Python 3.
Creating a Tuple
To create a tuple, the following syntax is used:
my_tuple = ("apple", "banana", "cherry")
In the example above, a tuple named my_tuple
has been created, that includes elements “apple”, “banana” and “cherry”. Tuples can also be created without parentheses, but it’s recommended to enclose them in parentheses because it makes them easier to read.
Tuples can also include elements of different types, such as numbers and strings.
mixed_tuple = (1, "apple", 3.5, True)
Accessing Tuple Elements
Elements in a tuple can be accessed using their index values. The index number starts from 0 for the first element and goes up to n-1
for the nth
element, where n
is the total number of elements in the tuple.
# access the first element in the tuple
my_tuple = ("apple", "banana", "cherry")
print(my_tuple[0]) # Output: apple
# access the third element in the tuple
print(my_tuple[2]) # Output: cherry
Negative indexing can also be used to access elements from the end of the tuple.
# access the last element in the tuple
print(my_tuple[-1]) # Output: cherry
# access the second last element in the tuple
print(my_tuple[-2]) # Output: banana
Tuple Methods
Tuples in python come with a few built-in methods that allow for easy manipulation of the tuple. These methods include:
count()
The count()
method returns the number of times a value appears in a tuple.
my_tuple = (1, 2, 3, 3, 3, 4, 5)
count_3 = my_tuple.count(3)
print(count_3) # Output: 3
index()
The index()
method returns the index of the first occurrence of a specified value.
my_tuple = ("apple", "banana", "cherry", "banana")
index_banana = my_tuple.index("banana")
print(index_banana) # Output: 1
Unpacking a Tuple
Tuple unpacking allows you to assign the values of a tuple to variables in one line of code. To unpack a tuple, we simply assign the tuple to a set of variables.
my_tuple = ("apple", "banana", "cherry")
(a, b, c) = my_tuple
print(a) # Output: apple
print(b) # Output: banana
print(c) # Output: cherry
If we want to ignore one or more elements during tuple unpacking, we can use the underscore symbol _
instead of the variable name.
my_tuple = ("apple", "banana", "cherry")
(a, _, c) = my_tuple
print(a) # Output: apple
print(c) # Output: cherry
Converting a Tuple to a List
Tuples are immutable, meaning their values cannot be changed. If you need to modify values in a tuple, you must first convert it to a list.
my_tuple = (1, 2, 3)
my_list = list(my_tuple)
print(my_list) # Output: [1, 2, 3]
Conclusion
Tuples are a useful data structure in Python programming language for storing and accessing elements that are ordered and unchangeable. It’s important to note that while tuples are similar to lists, they cannot be modified after they are created. This limitation makes tuples ideal for situations where read-only access is required. Hopefully, this article has provided helpful information on how to work with Python 3 tuples in your programs.