Tuples are a type of sequences like lists. The important thing to remember about tuples is that they are immutable.
Tuples are created using small brackets (). Just put any values separated by commas.
|
1 2 3 4 5 6 7 8 9 |
#### Creating Tuples #### # Creating numeric tuple tuple1=(10,20,30) print(tuple1) # Creating alphanumeric tuple tuple2=(1,2,3,4,'a','b') print(tuple2) |
Sample Output

Tuples are immutable
Once you create a tuple, you cannot change parts of it. You can overwrite it completely if you want although.
This is the reason why tuples are faster than lists. Because, python doesn’t have to maintain the change log metadata.
|
1 2 3 4 5 |
# Trying to change a value inside tuple # it will throw error as tuples are immutable tuple1=(10,20,30) tuple1[1]=40 |
Sample Output

When to use lists and when to use tuples in python?
When you need to store values dynamically, like running a loop and storing the results of each iteration, then use Lists. Because you can change a list dynamically.
When you need to iterate/reference values from a “fixed” set of values, then use tuples. e.g passing a list of hyper hyperparameters for grid search while parameter tuning in machine learning.
A tuple can contain a tuple!
Tuples are complex data structures, just like lists. Hence it can contain any value as one of the element. A number, string, list, tuple, dictionary etc.
Bottomline: A tuple can contain any python variable as an element.
In the below example, the complex tuple contains tuples as first two elements, a string as the third element and a list as the last element.
Now the tuples and list inside can also contain lists or tuples inside them! Creating inception of data structures! 🙂
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#### Creating Complex Tuple #### # Creating numeric tuple tuple1=(10,20,30) # Creating alphanumeric tuple tuple2=(1,2,3,4,'a','b') # Creating a list list1=[100,200,300] # Creating a complex tuple complexTuple=(tuple1, tuple2, 'hello', list1) print(complexTuple) |
Sample Output

Access elements from the tuple inside a tuple!
If you need to access the elements of the tuple inside the tuple or the list inside the tuple, then you need to use layer wise indexing. one set of square bracket [ ] for one layer.
For example to access the value ‘b’ from the complex tuple defined above, we need to first reach to the tuple present at second position using the index [1], then reach the value ‘b’ by providing the index for that tuple which is [5]. Hence there will be two set of brackets and indexes used, one for each layer. Always remember, python index begins from zero!
|
1 2 |
# Accesing tuple inside tuple complexTuple[1][5] |
Sample Output

Commonly used tuple commands
Once you create any variable in python, it inherits functions from its parent class. Same happens with a tuple as well. You can explore the functions associated with a tuple variable by using dot(.) and tab button.
Tuples cannot be altered dynamically, hence the number of associated functions are only two!

|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#### Common tuple commands tuple1=(10,20,30,30,50,50,50) tuple2=(100,200,300) # Finding the number of elements print('The number of elements in the tuple:',len(tuple1)) # Find the number of occurances of a value print('The number of occurances:',tuple1.count(50)) # adding two tuples NewTuple=tuple1+tuple2 print(NewTuple) # finding if a value is present in the tuple or not print(10 in tuple1) print(150 in tuple1) |
Sample Output

