Tuples
A tuple is an ordered sequence of data enclosed within opening and closing braces ( ) and the
elements are separated by commas(,). A tuple is immutable, it means any alteration, updating
are not allowed.
Eg.
>>>> ( ) -> Empty Tuple
>>>> t1 = tuple() -> Creating an empty tuple using tuple() method.
>>>> (2,4,6,8,10) -> tuple that has Integers only.
>>>> ('a','b', 'c', 'd') -> tuple that has letters only.
>>> ("The", "Cat", "Is", "Black") -> Tuple that has Strings Only.
>>> (24, 'X', 11.6, "Python") -> Tuple that has data from different-2 data types.
Tuple Indexing: Tuple indexing refers to the positioning of the elements in a tuple.
Forward 0 1 2 3 4 5
Indexing
Tuple 1 2 4 6 8 10 12
-6 -5 -4 -3 -2 -1 Backwar
d
Indexing
Creating a Tuple from Non-Tuple structures (string and list) by using tuple() method
Syntax:
variable = tuple(list or string)
Example:
L1 = [2, 4, 6, 8, 10]
T2 = tuple(L1)
Now T2 have (2, 4, 6, 8, 10)
Difference between a List and a Tuple
List Tuple
The elements are enclosed within square The elements are enclosed within braces ()
brackets [] separated by commas. separated by commas.
Elements of a list are mutable (i.e., Elements of a tuple are not changeable in its
changeable in its place) If a list L1 = [4, 2, 7, place.
9] then, L1[2] = 9 is a valid statement. If a tuple T1 = (4, 2, 7, 9) then, T1[2] = 9 is an
invalid statement.
Input a Tuple
1. Using tuple() function
Syntax:
Tuple variable = tuple(input())
Eg.
tuple1 = tuple(input(“Enter tuple elements”))
If entered : 12345 then tuple1 have (1, 2, 3, 4, 5)
2. Using eval() function
It is used to take tuple elements as is.
Syntax:
Tuple variable = eval(input())
Eg.
tuple1 = eval(input(“Enter list elements”))
If entered : (1, 2, 3, 4, 5) then tuple1 have (1, 2, 3, 4, 5)
Or eval () function is used in calculation
>>> eval (“12 + 5”)
Output is 17
Tuple Comparisons
Tuples can be compared by using relational operators (ie., ==, <, <=, >, >=, etc.). Value of each
index is compared.
Eg
Tuple T1 has 5 8 9 12
Tuple L2 has 5 15 9 12
If compared T1<T2 here value at index 1 in tuple1 is smaller then tuple2. It will return True.
Tuple operations
Creating a tuple with a single element
Eg, T1 = ('a’,) : A comma placed after the string will assign a single string to the tuple
Concatenating tuples
Syntax: Tuple1 + Tuple2 + ….. Tuplen
Example:
T1 = (1, 2, 3, 4)
T2 = (5, 8, 9)
T1 + T2
Output is : (1, 2, 3, 4, 5, 8, 9)
Replicating a tuple
Syntax: Tuple * Number of replications
Example:
T1 = (1, 3, 5)
T1 * 2
Output is: (1, 3, 5, 1, 3, 5)
Deleting a tuple
T1 = (1, 2, 3, 4)
del T1 : it will erase all the elements of the tuple.
Slicing a List
Examples:
T1 = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
A. T1 [2:6]
After slicing tuple will be: T1 = (30, 40, 50, 60)
B. T1 [2:-2]
After slicing tuple will be: T1 = (30, 40, 50, 60, 70, 80)
C. T1 [0:10:2] OR T1 [:10:2] OR T1 [0::2]
After slicing tuple will be: T1 = (10, 30, 50, 70, 90)
Traversing a List
1. Using tuple data
Example:
Tuple1 = ('a', 'e', 'i', 'o', 'u')
print('Elements of the tuple: ')
for x in Tuple1:
print(x)
2. Using the range of indices
Tuple1 = (2, 4, 6, 8)
print("Elements of the tuple:")
for i in range(len(Tuple1)):
print(Tuple1[i])
Membership operators (in and not in)
Syntax:
x in T1 : It will check whether an element 'x’ is present in the tuple 'T1’ or not and will return True
or False accordingly.
x not in T1 : It will check whether an element 'x’ is absent in the tuple 'T1’ or not and will return
‘True’ or ‘False’ accordingly.
Example:
T1 = (1, 3, 5, 7, 9)
print(3 in T1)
Output is True.
Built - in functions
len( )
This function is used to find the length of a tuple.
Syntax: <variable> = len(<tuple_name>)
Example:
>>> tuple1= (1, 2, 3, 4, 5)
>>> t1=len(tuple1)
>>> print("Number of elements in the tuple:",t1)
Output: Number of elements in the tuple: 5
index ( )
This function searches an element and returns the index of the first occurrence of an
element.
Syntax:
<Tuple>.index(A tuple element) OR
<Tuple>.index(tuple element, Initial index, Final index)
Example:
>>> T1 = (2, 4, 6, 8, 10)
>>> [Link](6)
Output: 2
>>> T1 = (2, 4, 6, 2, 8)
>>> [Link](2, 2, 5)
Output: 3
max ( )
This function is used to return the highest value among a set of values stored in a tuple.
Syntax:
max([data values in the tuple])
Example:
>>> max((2, 1, 5, 3, 4))
5
min ( )
This function is used to return the lowest value among a set of values stored in a tuple.
Syntax:
min([data values in the tuple])
Example:
>>> min((2, 1, 5, 3, 4))
1
count ( )
It is used for finding the frequency of an element among a set of elements stored in a
tuple.
Syntax:
<Tuple Object>.count(Value)
Example:
>>> T1 = (1, 2, 4, 2, 5, 1, 4, 2, 5, 2)
>>> [Link](2): It will find the frequency of 2 in the tuple T1
Output is: 4
sum ( )
This function is used to return the arithmetic sum of all the data values (numeric) stored
in a tuple.
Syntax:
sum([data values in the tuple])
Example:
>>> sum((2, 1, 5, 3, 4))
15
sorted (): This function is used to arrange a set of elements stored in a tuple, either in the
ascending or descending order. The sorted elements appeared as a list.
Syntax :
sorted(tuple) : It will sort the elements of a given tuple in the ascending order.
sorted(tuple, reverse = True) : It will sort the elements of a given tuple in the descending order
For eg
T1= (6, 3, 2, 7, 4)
sorted(T1)
Output is: [2, 3, 4, 6, 7]
Nesting a tuple
Nesting a tuple means using a tuple within another tuple.
For eg,
T1 = (1, 2, 3, (6, 8), 14, 15)