Python Tuple Operations
In Python,there are 5 basic operations: finding length, concatenation, repetition, membership and iteration operations can be performed on any sequence may be it is a string, list, tuple or a dictionary.
To find length of a tuple, we can use len() function. This returns the number of elements in the tuple.
student = (30, 'Abhinav Siddarth', 74,85,56,52,36)
We can concatenate or join two tuples and store the result in a new tuple. For example, the student paid a fees of Rs. 25,000.00 every year for 4 terms, then we can create a 'fees' tuple as:
fees = (25000.00,)*4 #repeat the tuple elements for 4 times.
Difference b/w fees = (25000.00,)*4 and fees = (25000.00)*4
Now, we can concatenate the 'student' and 'fees' tuples together to form a new tuple 'student1' as:
student1 = student+fees
Membership in Tuple
name in student1
Tuple operation functions:
|len()||len(tpl)||Returns the number of elements in the tuple.|
|min()||min(tpl)||Returns the smallest element in the tuple.|
|max()||max(tpl)||Returns the biggest element in the tuple.|
|count()||tpl.count(x)||Returns how many times the element 'x' is found in tpl.|
|index()||tpl.index(x)||Returns the first occurrence of the element 'x' in tpl. Raises ValueError if 'x' is not found in the tuple.|
|sorted()||sorted(tpl)||Sorts the elements of the tuple into ascending order.sorted(tpl,reverse=True) will sort in reverse order.|