## Tuple

- The Python Tuple is almost similar to Python List except that the Tuples are immutable and Lists are mutable. It means, Once we declare the Tuple we can’t change the values or items inside the tuple, something like Constant keyword in other programming languages.
- A Python Tuple is a sequence of multiple values in an ordered sequence.
- Since it is immutable, Traversing Tuple is much faster than List.
- Tuples are declared using Open and Closed Parenthesis
**( )** - Unlike Python Strings, Tuple allow us to store different types of data such as integer, float, string etc.

Tuple_Name = () is an empty Tuple that contains no values.

Integer_Tuple = (1, 2, 3, 4, 5) is an integer tuple that contains five integer values.

String_Tuple = (‘apple’, ‘Orange’, ‘Grape’, ‘Mango’) is a string tuple that contains four string values.

Mixed_Tuple = (‘apple’, 2, 3.50, ‘Mango’) is a mixed tuple that contains one integer, one float and two integer values.

Nested_Tuple = (‘Python’, ‘Tutorial’, (1, 2, 3) ) is an example of tuple inside another tuple (Nested Tuple)

List_Tuple = (‘Python’, ‘Tutorial’, [1, 2, 3] ) is an example of List inside a tuple

**TIP:** If you want to declare a Tuple with one item then, we have to specify comma after the item. For instance: **Tuple_Name = (245,). **If you forgot to add comma then it will an integer type (Not a Tuple)

## Accessing Python Tuple items

We can access the elements in a Python Tuple using indexes. Using index we can access each and every individual item present in the Tuples separately. Index value starts at 0 and end at n-1 where n is the Tuple size. For example, if a Tuple stores 5 elements then index starts at 0 and ends with 4. To access 1^{st} value use Tuple_name[0] and to access 5^{th} value then use Tuple_name[4]. Lets see the example for better understanding:

x = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Positive Indexing print(x[0]) print(x[4]) print(x[8]) print('=======\n') # Negative Indexing print(x[-2]) print(x[-4]) print(x[-8]) print('=======\n') # Accessing Nested Tuple Items Mixed_Tuple = ((1, 2, 3), [4, 5, 6], 'Python') print(Mixed_Tuple[0][0]) print(Mixed_Tuple[1][0]) print(Mixed_Tuple[2][0]) print(Mixed_Tuple[2][4])

**TIP:** If you are using the Negative numbers as index then, Python will start looking for items from right to left

**OUTPUT**

### Iterating Python Tuple

The Python For Loop is the most common way to traverse the items in a tuple. Following statement will help us to iterate the tuple and print the items inside the Fruits tuple.

Fruits = ('Apple', 'Orange', 'Grape', 'Banana', 'Strawberry','Cherry', 'Kiwi') for Fruit in Fruits: print(Fruit)

**OUTPUT**

## Performing Arithmetic Operations on Python Tuple

In this example we will show you, How to use Python Arithmetic Operators on Tuple to perform arithmetic Operations

X = (1, 2, 3) Y = (4, 5, 6) Sum = X + Y print(Sum) # Using * Operator Z = X * 3 print(Z)

**OUTPUT**

From the above screenshot you can observe that,

**+**operator is concatenating the python Tuple*****operator is repeating the tuple for given number of times. Here it is three times

## Slicing Python Tuple

In python Slice, the first integer value is the index position where the slicing will start and the second integer value is the index position where the slicing will end.

**TIP:** Slicing will go up to second integer value but it will not include the value at this index position. For instance, if we specify tuple_exmp[1:4] then slicing will start at index position 1 and ends at index position 3 (not 4).

x = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Slicing the Tuple using two indexes a = x[2:6] print("Both Indexes = ", a) # Slicing the Tuple using First indexes b = x[:6] print("No First Index = ", b) # Slicing the Tuple using Second indexes c = x[2:] print("No Second Index = ", c) # Slicing the Tuple without using two indexes d = x[:] print("No Indexes = ", d) # Slicing the Tuple using Negative indexes e = x[-3:] print("Negative First Index = ", e) # Slicing the Tuple using Negative indexes f = x[:-2] print("Negative Second Index = ", f)

**OUTPUT**

From the above screenshot you can observe that,

- If you omit the first index then, Slicing will start from the beginning
- If you omit the second index then, Slicing will start from the first index and continue to the last
- If you are using the Negative numbers as index then, Slicing starts from right to left

## Altering Python Tuple items

Python will not allow us to alter the items inside the Tuple. Let see what happens when we do that?

From the above screenshot you can observe that,

- Within the First statement, We are assigning new value at index position two. Since, Tuple is immutable, Output is throwing an Type Error.
- Within the Second statement, We are assigning new value to the tuple inside the tuple at index position one. Although it is a Nested tuple, output is throwing an Type Error because Tuple is immutable.
- Within the Third statement, We are assigning new value at index position [1][1] and that will be List inside the Tuple. Since, List is mutable we successfully changed the value from 5 to 14

## Python Tuple Methods

Python provides following methods to perform on Python Tuple

**Tuple():**This method will help us to convert the String or Lists into Tuple

### Python Tuple sum

Tuple sum function is to calculate the sum of all items inside the given Tuple

# Python tuple sum function x = (10, 20, 30, 40, 50, 60, 70, 80, 90) print("Tuple : ", x) total = sum(x) print("The Sum of Integer Tuple is : ", total) numbers = (9, -5, 7, 0, 24, -1, 2, 10) print("\nNumbers Tuple : ", numbers) print("The Sum of a Numbers Tuple is : ", sum(numbers))

**OUTPUT**

### Python Tuple min

Tuple min function help us to find Smallest item in a Tuple

# Python tuple min function fruits = ('Banana', 'Orange', 'Blackberry', 'Apple', 'Kiwi', 'Grape') print("Tuple : ", fruits) minimum = min(fruits) print("The Minimum Value in Fruits Tuple is : ", minimum) numbers = (9, -5, 7, 0, 24, -1, 2, 10) print("\nNumbers Tuple : ", numbers) print("The Minimum Value Numbers Tuple is : ", min(numbers))

**OUTPUT**

### Python Tuple max

Tuple max function help us to find Largest item in a Tuple

# Python tuple max function fruits = ('Banana', 'Orange', 'Blackberry', 'Apple', 'Kiwi', 'Grape') print("Tuple : ", fruits) maximum = max(fruits) print("The Maximum Value in Fruits Tuple is : ", maximum) numbers = (9, -5, 7, 0, 24, -1, 2, 10) print("\nNumbers Tuple : ", numbers) print("The Maximum Value Numbers Tuple is : ", max(numbers))

**OUTPUT**

### Python Tuple len

Tuple Len method help us to calculate Length of a Tuple (Number of items in a Tuple)

# Python tuple len function fruits = ('Apple', 'Banana', 'Orange', 'Blackberry', 'Kiwi', 'Grape') print("Tuple : ", fruits) length = len(fruits) print("The length of a Fruits Tuple is : ", length) numbers = (9, -5, 7, 0, 24, -1, 2, 10) print("\nNumbers Tuple : ", numbers) print("The length of a Numbers Tuple is : ", len(numbers))

**OUTPUT**

### Python Tuple sorted

Tuple sorted() method help you to sort the tuple (items in a tuple) in Ascending order

# Python tuple sorted function fruits = ('Banana', 'Orange', 'Blackberry', 'Apple', 'Kiwi', 'Grape') print("Old Tuple : ", fruits) new_fruits = sorted(fruits) print("Sorted Tuple : ", new_fruits) numbers = (9, -5, 7, 0, 24, -1, 2, 10) print("\nNumber Tuple : ", numbers) new_numbers = sorted(numbers) print("Sorted Numbers Tuple is : ", new_numbers)

**OUTPUT**

### Python Tuple index

Tuple index(x) function return the index position of a specified value

# Python tuple index function fruits = ('Banana', 'Orange', 'Blackberry', 'Apple', 'Kiwi', 'Grape') print("Tuple : ", fruits) print("The index position of Apple : ", fruits.index('Apple')) print("The index position of Orange : ", fruits.index('Orange')) print("The index position of Kiwi : ", fruits.index('Kiwi')) numbers = (9, -5, 7, 0, 24, -1, 2, 10) print("\nNumber Tuple : ", numbers) print("The index position of -1 : ", numbers.index(-1)) print("The index position of 0 : ", numbers.index(0)) print("The index position of 10 : ", numbers.index(10))

**OUTPUT**

### Python Tuple count

Tuple count(x) function help you to count, How many times the specified value is repeated inside the tuple

# Python tuple count function fruits = ('Banana', 'Kiwi', 'Grape', 'Apple', 'Kiwi', 'Grape') print("Tuple : ", fruits) print("Number of Times Apple is Repeated : ", fruits.count('Apple')) print("Number of Times Kiwi is Repeated : ", fruits.count('Kiwi')) print("Number of Times Grape is Repeated : ", fruits.count('Grape')) numbers = (2, -5, 7, 9, 2, -5, 2, 10) print("\nNumber Tuple : ", numbers) print("Number of Times 2 is Repeated : ", numbers.count(2)) print("Number of Times -5 is Repeated : ", numbers.count(-5)) print("Number of Times 10 is Repeated : ", numbers.count(10))

**OUTPUT**

In the below program, we are applying all the list methods. This might help you to save the image to see all the functions.

Fruits = ('Apple', 'Orange', 'Banana', 'Kiwi', 'Grape', 'Blackberry') x = (9, 4, -5, 0, 22, -1, 2, 14) #Finding Sum of all item in a Tuple using Python Sum() Method print('Sum of all items in a x Tuple = ', sum(x)) #Calculating Length of a Tuple using Python len() Method print('Length of a Fruits Tuple = ', len(Fruits)) print('Length of a x Tuple = ', len(x)) #Finding Minimum item in a Tuple using Python min() Method print('Minimum item in a Fruits Tuple = ', min(Fruits)) print('Minimum item in a x Tuple = ', min(x)) #Finding Maximum item in a Tuple using Python max() Method print('Maximum item in a Fruits Tuple = ', max(Fruits)) print('Maximum item in a x Tuple = ', max(x)) # Sorting the Tuple using Python Sorted() Method print('After Sorting Fruits Tuple = ', sorted(Fruits)) print('After Sorting x Tuple = ', sorted(x)) # Index position of an item in a Tuple using Python index() Method print('The Index position of Banana = ', Fruits.index('Banana')) print('The Index position of -1 = ', x.index(-1)) # Counting items in a Tuple using Python count() Method y = (9, 4, 1, 4, 9, -1, 2, 4) print('Number of Times 4 is repeated = ', y.count(4)) print('Number of Times 9 is repeated = ', y.count(9)) # Converting List into Tuple z = [1, 2, 3, 4, 5] print(tuple(z))

**OUTPUT**

Thank You for visiting Our Blog