The python tuples are sequences that are used to store a tuple of values of any type. Python tuples are immutable i.e. you cannot change the elements of a tuple in place; Python will create a fresh tuple when you make changes to an element of a tuple. Tuple is a type of sequence like string and lists but it differs from them in the way that lists are mutable but strings and tuples are immutable.
Types of Tuples
The tuple is a standard datatype of Python that can store a sequence of values belonging to any type. The tuples are depicted through parenthesis i.e., round brackets.
() #tuple with no member, empty tuple
(1, 2, 3) #tuple of integers
(1, 2.5, 3, 4.5, 7.4, 9) #tuple of numbers(integers and floating point)
(‘a’,’b’,’c’) #tuple of characters
(‘a’, 1,’b’, 4,’d’) #tuple of mixed value types
(‘one’, ‘two’, ‘three’) #tuple of strings
Creating a tuple is similar to list creation, but here we need to put a number of expressions in parentheses. That is, use round brackets to indicate the start and end of the tuple, and separate the items by commas.
(1, 2.5,6.8, 0.2,5)
Thus, to create a tuple you can write in the form given below:
This construct is known as a tuple display construct.
Tuples are immutable sequences having a progression of elements. Thus, other than editing items, you can do all that you can do with lists. Thus, you can access the tuple elements just like you access a list’s or a string’s elements
Tuple[i] will give you ith index ;
Tuple[a:b] will give you elements between indexes a to b-1.
- Length- Function len(T) returns the number of items(count) in the tuple T.
- Indexing and Slicing –
T[i] returns the item at index i
T[i:j] returns a new tuple, containing the objects between i and j excluding index j.
- Membership operators- Both ‘in’ and ‘not in’ operators work in tuples, ‘in’ tells if an element is present in the tuple or not and ‘not in’ does the opposite.
- Concatenation and replication operators + and * – The + operator adds one tuple to the end of another. The * operators repeats a tuple.
Joining Tuples– It is very easy just like you perform addition. The + operator, the concatenation operator, when used with two tuples, joins two tuples.
tp1=(1,2,3) tp2=(4,5,6) tp1+tp2
- Repeating or Replicating Tuples– Like strings and lists you can use * operator to replicate a tuple specified number of times.
- Slicing the Tuples– Tuples slices are the sub parts of the tuple extracted out.
tp1=(10,12,14,20,22,24,30,32,34) seq=tp1[3:6] seq
- Delete tuple– The del statement of python is used to delete elements and objects but as we know tuple is immutable, which means that individual elements of a tuple cannot be deleted.
del t1 print(t1)
NameError: name ‘t1’ is not defined
Tuple functions and methods
- len() – This method returns length of the tuple, i.e., the count of elements in the tuple.
- max()– This method returns the element from the tuple having maximum value.
- min()– This method returns the element from the tuple having minimum value.
- index()- This returns the index of an existing element of a tuple.
- count()- This method returns the count of a number element/object in a given tuple.