Loading, please wait...

A to Z Full Forms and Acronyms

All About Tuples in Python | Python Tutorials

this article is all about tuples , operations that we can perform on tuples, built in functions etc.

What is Tuple in Python?

A tuple is a collection that is ordered and unchangeable(immutable). These are sequences just like lists. The difference between tuple and list is that tuples are immutable whereas lists are mutable and another difference is that in Python tuples are written with round brackets but lists are written with square brackets.

How to Create a Tuple in Python?

Creating a tuple is very simple, its like putting commas separated values and optionally we can put these comma-separated values between round parenthesis lets see through example code.

tup1 = ("english", "hindi", "maths")
tup2 = (23, 56, 90, 40)

tup3 = ‘apple’ , ‘mango’ , ‘orange’ , ‘papaya’

How to Access Tuple Items?

We can access tuple items by referring to the index number, inside square brackets lets see through example code.

Example:
Print the third item in the tuple:

tuple1 = (‘a’, ‘b’, 1, 10)
print(tuple1[2])
#output: 1

Negative Indexing in Tuple

Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second-last item and so on, let's see through an example code...

Example:
Print the last item of the tuple

tuple1 = ("a", "banana", 3 , "cherry")
print(tuple1[-1])

#output: cherry

Range of Indexes

We can specify a range of indexes by specifying where to start and where to end the range. When specifying a range, the return value will be a new tuple with the specified items let's see through the example code.

Example:
Return the third, fourth, and fifth items...

tuple1 = ('python','java' , 'C' , 'C++' , 'php', 'kotlin' , 'ruby' , 'pascal' , 'perl')
print(tuple1[2:5])

#output:('C', 'C++', 'php')

Note: The search will start at index 2 (included) and end at index 5 (not included).

Range of Negative Indexes

Sometimes we want to start the search from the end of the tuple then we can specify negative indexes.

Example:
This example returns the items from index -3 (included) to index -1 (excluded)

tuple1 = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(tuple1[-3:-1])

#output:('kiwi', 'melon')

Change Or Update Tuple Values

Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called. But there is a workaround. You can convert the tuple into a list, change the list, and convert the list back into a tuple.

Example
Convert the tuple into a list to be able to change it:

fruits = ("apple", "banana", "cherry")
y = list(fruits)
y[1] = "kiwi"
x = tuple(y)

print(x)

#output:('apple', 'kiwi', 'cherry')

Loop Through a Tuple

Loop through the tuple items by using "for" a loop.

Example:
Iterate through the items and print the values:

tuple1 = ('python','java' , 'C' , 'C++' , 'php', 'kotlin' , 'ruby' , 'pascal' , 'perl')

for x in tuple1:
  print(x)

#output:python
java
C
C++
php
kotlin
ruby
pascal
perl

Delete Or Remove Tuple elements

Removing individual tuple elements is not possible. But we can delete the entire tuple using a del keyword.

tuple1 = ('python', 'java', 'C', 'C++', 'php', 'kotlin', 'ruby', 'pascal', 'perl')
print(tuple1)
del  tuple1
print(tuple1)

#output:
('python', 'java', 'C', 'C++', 'php', 'kotlin', 'ruby', 'pascal', 'perl')
Traceback (most recent call last):
  File "C:/Users/Mousmi/PycharmProjects/HelloWorld/tuple.py", line 110, in <module>
    print(tuple1)
NameError: name 'tuple1' is not defined

This will raise an exception because using del tuple1 Tuple does not exist anymore.

The operation that can we can perform on tuples

Tuples respond to string operators like + and *. i.e tuple can respond to these operators for concatenation and repletion but the result will also be a tuple, not a string.

Example:

Length of the tuple:

 Code:      len((1, 2, 3, 4, 5)

 Output:   5

Membership:

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

  Output:     True

Repetition:

  Code: (‘python’)*4

  Output: (‘python’, ‘python’, ‘python’, ‘python’,)

Iteration:

Code:

tuple1 = ("python”, “java” , “C” , “C++” , “php”)

for x in tuple1:
  print(x)

Output:

    python

     java

     C

     C++

     php

Concatenation:

Code: (1,2,3,4) + (9,8,7)

Output:

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

Indexing, slicing, and Matrixes in Tuples:

Because tuples are sequences, indexing and slicing work With tuples nicely.

Built-in Functions in Tuples :

  • Cmp(tuple1,tuple2): It compares elements of both the tuples.
  • Len(tuple): It returns the length of the tuple.
  • Max(tuple): It returns an element from tuple with max value.
  • Min(tuple): It returns an element from tuple with min value
  • Tuple(sequence): It can be used to convert a list into a tuple.
A to Z Full Forms and Acronyms

Related Article