A tuple is an ordered collection of items, meaning that each item has a fixed position in the tuple.
A tuple is immutable, meaning that once you create a tuple, you cannot change the items it contains. You cannot add, modify, or remove items from a tuple.
Tuples allow duplicates, which means you can have multiple copies of the same item in a single list.
Tuples are defined using parentheses ( ) and the items are separated by commas. When you print a tuple in Python, the parenthesis are used to enclose the items in the tuple, which helps to identify it.
The items in a tuple can be of different types, such as integers, floats, strings, or even other tuples.
The choice between a list and a tuple will often depend on whether you need to change the items in the collection or store data that should not be modified.
To create a tuple in Python, you can use parentheses () to enclose a comma-separated list of values
# example 1 : a tuple of integers
tuple1 = (5, 8, 8, 7, 3 )
# example 2 : a tuple of strings
tuple2 = ('a', 'e', 'i', 'o', 'u',)
# example 3 : another tuple of strings
tuple3=('car', 'bag', 'shirt', 'bed', 'fork', 'cup', 'spoon')
# example 4 : a tuple of strings and integers
tuple4=(5,2,'r','r',10,'t')
# example 5 : a tuple of tuples
tuple5=(tuple1,tuple2)
print(tuple5) # prints ((5, 8, 8, 7, 3), ('a', 'e', 'i', 'o', 'u'))
To create a tuple with a single element, you must include a comma after the element, even if there is only one element in the tuple. This is because Python interprets a pair of parentheses as a call to a function if there is no comma within the parentheses.
The first example below, tuple1=(5,), creates a tuple containing a single element. The comma after the element is necessary to indicate that this is a tuple, rather than just a parenthesized expression. This is confirmed by the output of type(tuple1). On the other hand, the second example, other=(5), creates a simple parenthesized expression. In this case, there is no comma separating any elements, so Python does not interpret it as a tuple. Instead, the output of type(other) is indicating that other is just an integer value.
tuple1=(5,)
print(type(tuple1)) # prints '<class 'tuple'>'
other=(5)
print(type(other)) # prints '<class 'int'>'
A tuple can even be created without using parentheses :
a = 1, 2
print(type(a)) # prints '<class 'tuple'>'
a = 4,
print(type(a)) # prints '<class 'tuple'>'
print(a) # prints (4,)
For this reason, it is important to remember that when a function returns multiple objects, they are stored in a tuple, and you will need to use the tuple's indices if you want to retrieve the returned objects individually.
def a_function():
return 1, 2, 3
print(a_function()) # prints (1, 2, 3)
print(type(a_function())) # prints '<class 'tuple'>'
print(a_function()[2]) # prints 3
You can use tuples to perform common mathematical set operations, such as finding the minimum and maximum value, or the total sum of a set of numbers
numbers=(1,3,9,5)
# find the minimum value in the tuple
print(min(numbers)) # prints 1
# find the maximum value in the tuple
print(max(numbers)) # prints 9
# find the sum of all values in the tuple
print(sum(numbers)) # prints 18
You can use the '+' operator to concatenate tuples
# example 1 : concatenate two tuples
numbers = (1, 2, 3)
letters = ('a', 'b', 'c')
newtuple1 = numbers + letters
print(newtuple1) # prints (1, 2, 3, 'a', 'b', 'c')
# example 2 : concatenate three tuples
colors=('green','red')
newtuple2 = numbers + letters + colors
print(newtuple2) # prints (1, 2, 3, 'a', 'b', 'c', 'green','red')
You can access the elements of tuple elements using their index within square brackets [], just like with a list. In python, indexing of sequences starts at 0. This means that the first element in a sequence is at index 0, the second element is at index 1. This is a common convention in programming languages. Indexing stops at the right limit of the indexes passed as arguments ( exclusive )
fruits = ('apple', 'banana', 'cherry', 'mango', 'pear')
print(fruits[1:3]) # prints ('banana', 'cherry')
print(fruits[0]) # prints apple
Common opertors :
tuple1 = ('a',)
tuple2 = ('2',)
print(tuple1 + tuple2) # prints ('a', '2')
print(tuple1 * 3) # prints ('a', 'a', 'a')
print('a' in tuple1) # prints True
print('b' in tuple1) # prints False
print('a' not in tuple1) # prints False
print(tuple1[0]) # prints 'a'
There are relatively few methods available for Python tuples, as compared to other collection types such as strings and lists.
# find the index of an element in a tuple
colors = ('red', 'green', 'blue', 'yellow', 'orange')
print(colors.index('blue')) # prints 2
# count the number of occurrences of an element in a tuple
numbers = (1, 2, 3, 2, 4, 5, 2)
print(numbers.count(2)) # prints 3