In Python, there are different data types that we use to store data collections. The are commonly four data types for this purposes; list, tuple, set and dictionary. In this lesson, we will focus on one of these data types, Python Tuples.
A Python Tuple can be created with round brackets. We can also crate tuples with tuple() constructor.
Tuples seems like python lists but there are some common differences between python lists and tuples. For example we can change Python Lists, but we can not change Python Tuples. We will talk about these differences in python tuple vs list lesson.
You can check these Tuple Lessons:
In a tuple, we can use different members or same members like python lists.
Here, we will give different example to learn Python Tuples better. Let’s start!
Below, we will create a tuple with has three members:
The output of this code will be like below:
You can practice below!
You can also watch the video of this lesson.
We can also create this tuple with tuple() constructor like below:
Here, do not forget the double round brackets. This method seems a little unnecessary but it is one of the way to create Python Tuples.
Let’s do another examples with numbers.
The output of this Python Tuples example will be like below:
Like Python Lists, Python Tuples has indexes to access specific member of the tuple. This indexes starts from 0 as it is in python lists.
Index  First member
Index  Second member
Index  Third member
Index  Fourth member
And so on.
Let’s access the second member of the above tuples.
The output will be:
The output will be:
We can also use negative numbers as index numbers. And if we use negative numbers, then we start to count from the end of the tuple in reverse order. The last item in the tuple has index -1.
The output of this python tuple code will be:
We can also access a series of items in a tuple with the help of colon ( : ) operator.
Here, the left side of the colon ( : ) shows the starting index and right side of the colon ( : ) shows the ending index. But the item that resides in the enfding index is not included.
We will also give different example to this python tuple access in the releted lesson.
By the way, according to python, tuples are defined as objects with data type ‘tuple’. So, if you would like to see this, you can use the below python code:
The output of this python code will be like below as expected:
We can use some methods with Python Tuples. Let’s do some examples of these methods used with Python Tuples.
In the below example, we will count the number of one member in the tuple. In other words, we will count 10 in the complete tuple with python count method.
The output of this code will be:
Now, let’s use python len function to measure the length of a Python Tuple.
The output of this python code will be:
We can use tuples with also python loops. In the below example, we will use “for loop” to print each member of the tuple to each line.
The output of this tuple loop example will be like below:
As we have mentioned at the beginning of this lesson, we can not change tuples in python. For example we can not do this with a usage like python tuple append. But there is always a way to do something. So, here we can do a trick to do this sometimes. For example, we can change a tuple to a list with list function. We can add a new member to this list and then we can convert the list to a tuple again. Here, the tuple can not be changes again but with a trick we achieve to add a memebr to it.
In the output of this code, you will see the new member:
In this Python Tuples lesson, we have learned how to create Python Tuples basically. In the following lessons, we will focus to the details of tuples and we will give more examples on one of the data types of python.
Leave a Reply