In this Tutorial we are going to see how to use tuples in Python. Tuples are the most versatile, useful data types in Python. In almost any non-trivial Python program, you will discover them.
We’ll be covering the following topics in this tutorial:
What are tuples in python
The tuples in python are very similar to lists. The tuple is an orderly and unchangeable collection. Allow duplicate members. It used to store the collection of elements in a single variable. Still, there is a significant difference between them, and that difference is tuples are immutable. Once tuples created, the content in the tuple cannot be changed.
How to creating tuples in python
I’m going to declare a variable called X, and I will assign some values to it. In case of a list, we have seen we used the square brackets in tuples we use these parentheses to store the list of elements. For example
x = (1, 5, 3, 4, 8)
Accessing Values in Tuples
in order to accessing values in tuples you can use
#!/usr/bin/python x = (1, 5, 3, 4, 8) print(x)
When the above code is executed, it produces the following result
(1, 5, 3, 4, 8)
It’s going to print the tuple. As I said tuples are similar to lists in some ways, you can call a tuple element by index in the tuple, so their index arranges all the tuple elements and when you want to, for example, get the first element here you give the index 0.
#!/usr/bin/python x = (1, 5, 3, 4, 8) print(x[0])
When the above code is executed, it produces the following result
Output: 1
It’s going to give you the value which saved at index 0. let’s get the value which saved at index 4.
#!/usr/bin/python x = (1, 5, 3, 4, 8) print(x[4]) Output: 8
You can see it gives 8, which saved at index 4.
when I try to give an index which is not there, for example, 100.
#!/usr/bin/python x = (1, 5, 3, 4, 8) print(x[100]) Output: IndexError: tuple index out of range
It’s going to give us error which says IndexError: tuple index out of range
.
Updating Tuples
As I said tuples are immutable, you cannot change a tuple’s content if they initialized once.
let’s try to test this. I’m going to just asign the value at index zero which is let’s see
#!/usr/bin/python x = (1, 5, 3, 4, 8) x[0] = 2 print(x[0]) Output: TypeError: 'tuple' object does not support item assignment
You can see it TypeError: 'tuple' object does not support item assignment
. You cannot assign any other value to a tuple element, if once initialized. That also means that you cannot use some of the functions which you used with the list in the tuples, for example, you cannot remove any element, you cannot append any element, you cannot change any element for example
There are few available methods with the tuple and most important is the count method so let’s try to use the count method, and I want to know how many numbers of 8 are there in this tuple.
#!/usr/bin/python x = (1, 5, 3, 4, 8) print(x.count(8)) Output: 1
and it will give me 1 because 8 is only present once in this tuple. If it is present twice in this tuple here, you will see the result. Now to get the tuple’s length, you can use the inbuilt function, which is a len()
.
#!/usr/bin/python x = (1, 5, 3, 4, 8) print(len(x)) Output: 5
You can save multiple data type values in lists. Let’s see if we can do the same with tuples or not. I’m going to give the parentheses. I will provide the first value as an integer, second value as a string, third value, for example, the decimal value. It’s going to give you the length of the tuples.
y = (1, 'max', 1.6)
and I’m going to get the value of y, and you can see it’s possible to save multiple data type values in our table.
#!/usr/bin/python y = (1, 'max', 1.6) print(y)
now I have declared two tuples one is X and other is Y. so let’s print X and Y’s once again very quickly and now what we want to do is we want to use a concatenation operators so as I said tuples are immutable but let me define a third tuple. here and I want to add
Z = x + y
Here I want to assign the value to z. is it possible let’s check. I’m going to see the contents in the Z variable. You can see when you use a plus operator or concatenation operator. It’s going to join these two tuples and create a new tuple with the combination of both these tuples’ values.
#!/usr/bin/python x = (1, 5, 3, 4, 8) y = (1, 'max', 1.6) z = x + y print(z) Output: (1, 5, 3, 4, 8, 1, 'max', 1.6)
Python tuple with only one element
Now there is one exciting thing, which you can do with a tuple. which is you can declare a variable and let me define a tuple with only one element, and I’m going to give
#!/usr/bin/python a = ('hi',) * 5 print(a)
Here and then after this element. I’m going to provide comma here, so this is important. I have provided this comma, and I haven’t offered the second element in the tuple, and I can use asterisks here, and I can use some value here, so let’s say I have provided 5
('hi', 'hi', 'hi', 'hi', 'hi')
Now you can see the hi
string is saved 5 times in a variable in the form of a tuple. you can once again get this value at index two, for example,
print (a[2])
and it’s going to give you the value which stored at index 2 which is hi itself.
Python Tuple max() Method
you can also use inbuilt function called max()
here to get the maximum value out of a tuples. We have provided X tuple here and you will be able to see
#!/usr/bin/python x = (1, 5, 3, 4, 8) print(max(x)) Output: 8
It will give us the maximum value stored in that tuples, and you can see here it is the maximum value inside the tuple.
Python Tuple min() Method
We can also get the minimum value using a min()
function, and it’s going to give you the minimum value. Which is stored inside the tuple
#!/usr/bin/python x = (1, 5, 3, 4, 8) print(min(x)) Output: 1
Delete Tuple Elements
you can use the del()
function to delete tuples. so let’s say, I want to delete this tuple, which is Z. I can write the name of the tuple after the del()
for example
#!/usr/bin/python del z
When I try to get the value, it will give me NameError: name 'z' is not defined
. To delete the tuple, you can use that del()
function.