What is Tuple?

If we explain what tuples are; Tuples are immutable dats types that can contain more than one data type, denoted by commas or parentheses.

How to Use Tuples?

Let’s show it with an example;

tuple = ("Python", "PHP", "Flutter", "Go")
('Python', 'PHP', 'Flutter', 'Go')

Or we can show it with commas;

tuple = "Python", "PHP", "Flutter", "Go"
('Python', 'PHP', 'Flutter', 'Go')

But there is a very important point that we need to pay attention to here, if we are going to use a comma and if the tuple has one element, when we do not put a comma at the end, the interpreter perceives it as a String (character array).

Let me show you right now;

tuple = "Python"
<class 'str'>

Let’s try using a comma;

tuple = "Python",
<class 'tuple'>

How is Tuple Access?

Accessing tuple elements are the same as accessing elements with Lists

tuple = "Python", "PHP", "Flutter", "Go"

Let’s do another example;

tuple = "Python", "PHP", "Flutter", "Go"
('PHP', 'Flutter', 'Go')

Tuple Methods

In the previous blog posts of the Python blog, I said that the tuple is an immutable data type, so the tuple can’t be added, deleted, etc. If you want, let’s show you with an example;

tuple = ("Python", "Flutter", "Go", "JavaScript", "PHP", "Java", "Python")
tuple[1] = "PHP"

We will come across a problem as follows. 'tuple' object does not support item assignment" tuple object does not support member assignment.

Let’s list the methods with the dir() function;

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__',
 '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
 '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__',
 '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__',
 '__subclasshook__', 'count','index']

Since methods such as __X__ are private methods, we will not look at them for now.

index Method:

The index method is a method that finds the index of the searched element in a tuple.


tuple = ("Python", "PHP", "Flutter", "Go")

Now, let’s do an application that questions whether there is an element in the tuple;

tuple = ("Python", "PHP", "Flutter", "Go", "Javascript")
element = input("Enter a element: ")
if element in tuple:
    print("The element you are looking for is in the index {}.".format(tuple.index(element)))
    print("The element you are looking for is not in the list")
Enter a element: PHP
The element you are looking for is in the index 1.

Enter a element: C#
The element you are looking for is not in the list

count Method

The Count method is a method that finds how many of the same element is in the tuple.

tuple = ("Python", "Java", "PHP", "JavaScript", "Python", "Go", "python")

What we need to pay attention to here is that although the element named Python appears three times in the list, we get two results. The reason is that Python is case-sensitive, so we have mentioned this as well.

Well, if you are asking how can I search without letter compatibility, we do these operations with meta characters that we will process in the future.