  Introduction to Python Tuple Concept with Practical Examples

Python Tuple Concept – Before moving ahead, let’s know about Python List

Tuple – It is a collection and contains elements in ordered and unchangeable form. It represents with round brackets.

Creation of Tuple

Example 1- Creating a Tuple.

```x = (1,2,3)
print(x)```

Allow Duplicates Items

Tuple Duplicates Items – Since, Tuple items are indexed therefore, it allows duplicates items.

```x = ('a', 'b', 'c', 'd', 'a', 'e')
print(x)```

Tuple Type()

Tuple Type() – In Python, Tuple is defined as class <tuple>

Example – Define Tuple Type()

```x = ('a' , 'b' , 'c' , 'd')
print(type(x))```

Tuple Data Types

Tuple Items – Tuple items can be any type of Data Types.

Example – Tuple items with data type str, int and boolean.

```x = ('a' , 'b' , 'c' , 'd')
y = (1 , 2 , 3 , 4 , 5)
z = (True , False)

print('Tuple str' , x)
print('Tuple int' , y)
print('Tuple Boolean' , z)```

Accessing Tuple Item

Access Tuple elements – Elements can be accessed by defining index number in the square brackets.

Example 2- Access element ‘c’ of Tuple.

```x = ('a', 'b', 'c', 'd')
print(x)```

Note: In beginning index number starts from zero (0).

Negative Indexing

Negative Indexing – It starts from end with the index number -1 and beginning with 0.

Example 3- Get second last element ‘c’ of Tuple.

```x = ('a' , 'b' , 'c' , 'd')
print(x[-2])```

Note:  In end index numbers starts from minus one (-1).

Indexing Range

Range of indexes – It specified range of index number to get particular element from start to end.  Specifying range returns new list of specified elements.

Example 4- Get particular elements from index number 1 to 2.

```x = ('Welcome' , 'in' , 'programming' , 'language')
print(x[1:3])``` As it is shown clearly that it returned particular elements from index number 1 to 2.

Note:  Starting index number (1) is included but ending number (3) is excluded.

Negative Indexing with Range

Range of negative indexes – It specified range of negative indexing number. It starts from end with index number -1.

Example 5- Start search from end and get particular elements from start to end.

```x = ('Welcome', 'in', 'programming', 'language')
print(x[-4:-1])```

Tuples are unchangeable, or immutable. Once a tuple created its value cannot be changed. But it is possible to convert a tuple into a list and vice versa.

Example 6- Convert a tuple into list.

```x = ('Welcome' , 'in' , 'programming' , 'language')
print("before updating Tuple type is:" , x)
print(type(x))

z = list(x)
print("After updating Tuple type" , z)
print(type(z))``` As it is shown clearly that type of Tuple has changed from to after using list method.

Loop in Tuple

Loop in Tuple – A loop in Python is considered as iterating throughout elements. A Tuple can loop through Tuple.

Example 7- Use of ‘for’ loop to looping through Tuple.

```z = ('Welcome' , 'in' , 'programming' , 'language')
for x in z:
print(x)``` As it shown clearly that every element is looped through ‘for’ loop and returned one by one.

Check if elements exist

Check element exits – To check particular element exits, use ‘in’ keyword.

Example 8- Check if element ‘programming’ exits.

```x = ('Welcome' , 'in' , 'programming' , 'language')
print('programming' in x)```

Tuple Length

Length of list – To get length of Tuple use ‘len’ function.

Example 9- Use len() function to get length.

```x = ('Welcome' , 'in' , 'programming' , 'language')
print(len(x))```

Tuple is immutable. Once a Tuple created its element cannot be changed.

Example 10- Checking if element can add in Tuple.

```x = ('Welcome' , 'programming' , 'language')
x = 'in'

print(x)```