What are tuples in python

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.
E.g.,
() #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 Tuples
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.
E.g.,
(2,4,6)
(‘acf’, ‘deb’)
(1, 2.5,6.8, 0.2,5)
Thus, to create a tuple you can write in the form given below:
T=()
T=(value, ….)
This construct is known as a tuple display construct.
Accessing Tuples
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
e.g.,
Tuple[i] will give you ith index ;
Tuple[a:b] will give you elements between indexes a to b-1.
Etc, etc.
- 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.
Tuple operations
Joining Tuples– It is very easy just like you perform addition. The + operator, the concatenation operator, when used with two tuples, joins two tuples.
e.g.,
tp1=(1,2,3) tp2=(4,5,6) tp1+tp2
Output=(1,2,3,4,5,6)
- Repeating or Replicating Tuples– Like strings and lists you can use * operator to replicate a tuple specified number of times.
E.g.,
tp1=(1,3,5) tp1*3
Output=(1,3,5,1,3,5,1,3,5)
- Slicing the Tuples– Tuples slices are the sub parts of the tuple extracted out.
Format:- seq=T[start:stop]
E.g.,
tp1=(10,12,14,20,22,24,30,32,34) seq=tp1[3:6] seq
Output=(20,22,24)
- 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.
E.g.,
t1=(5,6,7,8,9) t1
(5,6,7,8,9)
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.
Syntax:-
len(tuple)
- max()– This method returns the element from the tuple having maximum value.
Syntax:-
max(tuple)
- min()– This method returns the element from the tuple having minimum value.
Syntax:-
min(tuple)
- index()- This returns the index of an existing element of a tuple.
Syntax:-
tuplename.index(item)
- count()- This method returns the count of a number element/object in a given tuple.
Syntax:-
tuplename.count(object)