Updated February 21, 2023
Definition of Python 3 Tuple
Python 3 tuple lists Python objects that can’t be changed. Like lists, tuples are made up of sequences. The most significant distinction between tuples and lists is that tuples, unlike lists, cannot be modified. Parentheses are used in tuples, but square brackets are used in lists. Putting values of comma-separated into tuple is as easy as that. These comma-separated values can alternatively be put between parentheses if desired.
What is Python 3 tuple?
- Tuples are a variable that allows us to store many objects in one place. Tuple is one of four built-in Python data types for storing data collections; the other three are Dictionary, List, and Set, all of which have different properties and applications.
- Round brackets are used to write multiples. In python, tuple is analogous to the list. We can’t use float or other kinds because the index contains integers.
Python 3 Tuple access values
We can access the elements of a tuple in a number of different ways as follows.
1. Accessing tuples by using Indexing
- We use the index operator to retrieve an item in a tuple, where the index starts at 0.
- As a result, indexes range from 0 to 5 for a tuple with 6 items. An Index Error will be raised if we try to access the index form outside.
- The distinction is that once a tuple is assigned, we cannot change its elements, whereas we can change the list’s contents.
- The below example shows accessing tuples via indexing in python.
Code:
py_tup = ('A', 'Q', 'S', 'T', 'B', 'N')
print (py_tup [0])
print (py_tup [5])
Output:
2. Accessing tuples by using negative indexing
- Python’s sequences support negative indexing. The final item is represented by the index of -1, the second last item by the index of -2, and so on.
- The below example shows accessing tuples via negative indexing in python is as follows.
Code:
py_tup = ('A', 'Q', 'S', 'T', 'B', 'N')
print (py_tup [-1])
print (py_tup [-4])
Output:
3. Accessing tuples by using slicing
- The best way to visualize slicing is to think of the index as being in the middle of the elements, as seen below. If we wish to access a range, we’ll need the index that slices the tuple into slices.
- Using the slicing operator colon, we may access various things in a tuple. The below example shows accessing tuples via negative indexing in python as follows.
Code:
py_tup = ('A', 'Q', 'S', 'T', 'B', 'N')
print (py_tup [2:4])
print (py_tup [:-5])
Output:
Python 3 tuple delete
- The elements of a tuple can’t be changed. We can’t delete or remove entries from a tuple due to this.
- It is not feasible to remove single tuple elements. Of course, there’s nothing wrong with constructing a new tuple that excludes unwanted components.
- Tuples are immutable; thus, we can’t delete a single element from them. Using the del method, we can delete the entire tuple.
- The below example shows we cannot delete a single tuple value from an element. In the example below, we are trying to delete a single element but will show an error.
Code:
py_tup = ('A', 'Q', 'S', 'T', 'B', 'N')
del py_tup (tup1 [0])
print (py_tup)
Output:
- The below example shows deleting all elements from a tuple in python. In the below example, we are deleting the py_tup tuple.
Code:
py_tup = ('A', 'Q', 'S', 'T', 'B', 'N')
del py_tup
Output:
- The below example shows after deleting the tuple we cannot see the elements from tuple it will throw the error that tuple is not defined.
Code:
py_tup = ('A', 'Q', 'S', 'T', 'B', 'N')
del py_tup
print (py_tup)
Output:
Python 3 Tuple Operations
We can perform the below tuple operations in python as follows.
1. Tuple membership test
By using this operation of tuple in python we can check whether the item exists in tuple or not by using the keyword name as in.
Code:
py_tup = ('A', 'Q', 'S', 'T', 'B', 'N')
print('a' in py_tup)
print('A' in py_tup)
Output:
2. Iterating by using tuple
For iterating each item we are using for loop in python. The below example shows iterating by using tuple are as follows.
Code:
for stud_name in ('ABC', 'PQR'):
print ("Student", stud_name)
Output:
3. Creating tuple
The below example shows creating a tuple in python as follows. We are creating a tuple name as py_tup. Code:
Py_tup = ('ABC', 'PQR')
print (Py_tup)
Output:
4. Concatenating tuple in python
We can concatenate the two tuples in python by using “+”. In the below example, we are concatenating the py_tup1 and py_tup2 as follows.
Code:
Py_tup1 = ('ABC', 'PQR')
Py_tup2 = ('11', '12', '13')
print (Py_tup1)
print (Py_tup2)
print(Py_tup1 + Py_tup2)
Output:
5. Repeat the tuple elements
In python, we can repeat the tuple elements multiple times. The below example shows repeats the tuple elements as follows.
Code:
py_tup = ('tuple',)*3
print (py_tup)
Output:
Python 3 tuple Functions
Below is the function of the python 3 tuple as follows.
1. Len – Len function is used to find the length of tuples. The below example shows len function as follows.
Code:
py_tup = ('25', '35', '45')
print(len(py_tup))
Output:
2. Max – Max function is used to find max elements from tuples. The below example shows the max functions as follows.
Code:
py_tup = ('25', '35', '45')
print(max(py_tup))
Output:
3. Min – Min function is used to find the smallest element from tuples. The below example shows the min function as follows.
Code:
py_tup = ('25', '35', '45')
print(min(py_tup))
Output:
4. Tuple – This function will convert the list into a tuple. The below example shows tuple function examples as follows.
Code:
py_tup = tuple (('25', '35', '45'))
print (py_tup)
Output:
Conclusion
Tuples are a variable that allows us to store many objects in one place. The distinction is that once a tuple is assigned, we cannot change its elements, whereas we can change the list’s contents. We can’t use float or other kinds because the index contains integers.
Recommended Articles
This is a guide to Python 3 Tuple. Here we discuss the definition, What is python 3 tuple?, Examples with code implementation. You may also have a look at the following articles to learn more –