Site icon TestingDocs.com

Python Tuple Functions

Overview

Let’s learn Python tuple functions in this tutorial. Python has a sequence type called a Tuple that cannot be modified once created, making it an immutable data structure. Tuples are defined by enclosing elements in parentheses () and can store a sequence of different types of elements.

Python Tuple Functions

Python language also provides various tuple functions, which are as follows:

 

Python Tuple Function
Description
len() This function returns the total number of elements in the tuple.
min()  This function returns the smallest element in the tuple.
max()  This function returns the largest element in the tuple.
sum()
This function returns the sum of all the elements of the tuple.
sort() This function returns the sorted list of elements of the tuple.
all() This function returns true if all the elements of the tuple are true.
any() This function returns true if any element of the tuple is true.
enumerate() This function returns an enumerate object of the tuple.

 

Example

Let’s understand the function with the use of a sample Python program.

 

# Python program tuple functions
# Python Tutorials - www.TestingDocs.com
tup = (18,22,34,56,73,82,90)  # tuple initialization

print('Length of the tuple ={0}'.format(len(tup)))
print('Min element of the tuple ={0}'.format(min(tup)))
print('Max element of the tuple ={0}'.format(max(tup)))
print('Sum of elements in the tuple ={0}'.format(sum(tup)))

 

The immutability of tuples results in faster iteration compared to lists. Tuples are a fundamental data type in Python and are extensively used in various applications, particularly where immutability and data integrity are important.

Python Tutorials

Python Tutorial on this website can be found at:

https://www.testingdocs.com/python-tutorials/

More information on Python is available at the official website:

https://www.python.org

Exit mobile version