Basic functions to process Tuple

This blog describes some of the functions available in Python to process tuple.

Below is the list of function to process tuple

len()
min()
max()
count()
index()
sorted()

len() function

Returns number of elements in a tuple

Example

tup = (1,2,3,4,5,6)
print('Number of elements:',len(tup))

Output

Number of elements: 6

min() function

Returns smallest element from a tuple

Example

tup = (1,2,3,4,5,6)
print('Smallest element:',min(tup))

Output

1

max() function

Returns the biggest element from a tuple

Example

tup = (1,2,3,4,5,6)
print('Biggest element:',max(tup))

Output

Biggest element: 6

count() function

Returns number of times x element is found in a tuple

Example

tup = (1,2,3,2,2,5,6)
print('Number of time element appeared in a tuple:',tup.count(2))

Output

Number of time element appeared in a tuple: 3

index() function

Returns the first occurrence of the x element from a tuple. Provides the first index position of x element

Example

tup = (1,4,5,2,3,2,2,5,6)
print('First occurrence',tup.index(2))

Output

First occurrence 3

sorted() function

Sort the elements into ascending order.

Example

tup = (59,69,19,29,49,39)
print(sorted(tup))

Output

[19, 29, 39, 49, 59, 69]

Sorting elements into descending order with reverse set to true

tup = (59,69,19,29,49,39)
print(sorted(tup, reverse=True))

Output

[69, 59, 49, 39, 29, 19]
Advertisement

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s