The Best Way To Learn TUPLE IN PYTHON
By: Sumant Jha
Tags: Python, Python3, Python Programming, Tuples, Python Tuple

What is a Tuple in Python?

A tuple in python has an immutable data type, almost identical to the list of pythons and duplicate members in terms of indexing. It is a collection data type that stores python objects severed by commas.

Let me show you an example with will example explain how we can create or declare a tuple in python.

So ill just show you how to declare a tuple in python.

>>> a=(1,2,3,4,5)

>>> print(a)

(1, 2, 3, 4, 5)

take a variable and take few values inside the round bracket (parenthesis) and when I print we will get a tuple that is separated by a comma and inside the round bracket. But there is one more approach to this where you can also declare the tuple in python like this.

>>> a=1,2,3,4,5

>>> print(a)

(1, 2, 3, 4, 5)

You simply have to mention some value separated by commas and when I print an again and getting the same value as I was getting before. So you can follow both of these approaches to actually declare a Tuple in Python.

So this is how we can create a Tuple in Python.

Let’s take a look at the next topic that we have.

Accessing Elements in a Tuple

So accessing items in a tuple works similarly to a list. And we can access elements in the list using indexes. And you can specify the index value and it will return the item that stored a particular index value.

Now talking about Indexing It is a data structure technique to effectively retrieve information from a data structure. In Python, several data types support indexing like List or string, etc. So what happens let see for example we have Tuple with five natural numbers that have 1,2,3,4,5 so indexing will start at the value 0 (zero) where 1 will be stored and it will go until the Tuple that is the number 5. And the index value at number 5 will be 4. So in Python, we can use the negative index as well to access elements in Tuple or any other data type that support indexing.

Now I’ll show you how you can use index the values to access values in a Tuple.

>>> a[4]

5

So we have a here inside the square braces I’ll just specify the index number let's say 4 over here. Because we have 5 elements so it should ideally give me number 5.

Now talking about negative indexing so if I write -1 inside square brackets. What will be the value we got? I am getting the same value as 5. So when we talk about negative indexing is will start at 5. So negative index -1 will be at 5 and for 4 will be -2 and similarly as so on.

So now this is all about indexing. Now let me talk about slicing.

So slicing is basically a technique in which we use the slicing operator which is a colon and it is used to get a range of elements from a Tuple or any other data types that support indexing for accessing elements.

So let me just show you an example.

>>> b=(1,2,3,4,5,6,7,8,9)

>>> print(b)

(1, 2, 3, 4, 5, 6, 7, 8, 9)

This time I am taking variables to be distinct. And I’ll just specify the numbers from 1 to 9. So when I print this I have a Tuple that has a value from 1 to 9.

Now talking about slicing, It works similar to indexing only it just will have to add a slicing operator.

>>> b[0:]

(1, 2, 3, 4, 5, 6, 7, 8, 9)

So I’ll start that indexing number 0 and I want to go to the end of the Tuple. So when I print this, I am getting all the value inside the Tuple. So in this Tuple, the ending index is basically 8 so I’ll just write 8 over the end.

>>> b[0:8]

(1, 2, 3, 4, 5, 6, 7, 8)

Getting the value until the index number is 8. So you can see that number 9 not included here. So when we doing a slicing operation the index number that you write after the slicing operator.

Similarly, we can use negative indexes as well. I am getting the same values and I can use values like these also so just add -8.

>>> b[-8:]

(2, 3, 4, 5, 6, 7, 8, 9)

getting the same.

This is all about slicing. Now let’s click here to look at the next topic that we have.