Tuples
Tuples are basically a data type in python. These tuples are an ordered collection of elements of different data types. Furthermore, we represent them by writing the elements inside the parenthesis separated by commas. We can also define tuples as lists that we cannot change. Therefore, we can call them immutable tuples. Hence, tuples are not modifiable in nature. These immutable tuples are a kind of group data type. Moreover, we access elements by using the index starting from zero. There are some functions that we can directly perform in a tuple. Let us learn these tuple functions in detail.
Tuple Functions
There are some methods and functions which help us to perform different tasks in a tuple. Therefore, we can call them tuple functions. Furthermore, these tuple functions make our work easy and efficient. Besides, there are a number of functions such as cmp(), len(), max(), min(), tuple(), index(), count(), sum(), any(), all(), sorted(), reversed().
Furthermore, these functions help us to perform several kinds of tasks such as finding the length of a tuple, creating a tuple, finding the occurrence of an element in a tuple, etc. Let us learn about each one of these tuple functions in detail.
The tuple() Function
We can use the tuple() constructor or function to create a tuple. It basically performs two functions as follows:
- Creating an empty tuple if we give no arguments.
- Creating a tuple with elements if we pass the arguments.
For example,
>>>tup = tuple ((22, 45, 23, 78, 6.89))
>>>tup
(22, 45, 23, 78, 6.89)
>>> tup2 = tuple()
>>> tup2
()
The len() Function
This function returns the number of elements present in a tuple. Moreover, it is necessary to provide a tuple to the len() function.
For example,
>>>tup = (22, 45, 23, 78, 6.89)
>>> len(tup)
5
Browse more Topics Under Tuples and its Functions
- Immutable Tuples
- Creating Tuples
- Initialising and Accessing Elements in a Tuple
- Tuple Assignment
- Tuple Slicing
- Tuple Indexing
The count() Function
This function will help us to fund the number of times an element is present in the tuple. Furthermore, we have to mention the element whose count we need to find, inside the count function.
For example,
>>>tup = (22, 45, 23, 78, 22, 22, 6.89)
>>> tup.count(22)
3
>>> tup.count(54)
0
The index() Function
The tuple index() method helps us to find the index or occurrence of an element in a tuple. This function basically performs two functions:
- Giving the first occurrence of an element in the tuple.
- Raising an exception if the element mentioned is not found in the tuple.
For example,
Example 1: Finding the index of an element
>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
>>> print(tup.index(45))
>>> print(tup.index(890))
#prints the index of elements 45 and 890
2
7
Example 2:
>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
>>> print(tup.index(3.2))
# gives an error because the element is not present in the tuple.
ValueError: tuple.index(x): x not in tuple
The sorted() Function
This method takes a tuple as an input and returns a sorted list as an output. Moreover, it does not make any changes to the original tuple.
For example,
>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
>>> sorted(tup)
[1, 2, 2.4, 3, 4, 22, 45, 56, 890]
The min(), max(), and sum() Tuple Functions
min(): gives the smallest element in the tuple as an output. Hence, the name is min().
For example,
max(): gives the largest element in the tuple as an output. Hence, the name is max().
For example,
>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
>>> max(tup)
890
max(): gives the sum of the elements present in the tuple as an output.
For example,
>>> tup = (22, 3, 45, 4, 2, 56, 890, 1)
>>> sum(tup)
1023
Frequently Asked Questions (FAQs)
Q1. Why do we use tuple functions?
A1. There are some methods and functions which help us to perform different tasks in a tuple. Furthermore, these tuple functions make our work easy and efficient.
Leave a Reply