Loop Control Statements in C Introduction to HTML How to use the Github API The image tag, anchor tag and the button tag Ordered and Unordered Lists in HTML The division tag HTML Forms Tables in HTML Introduction to C Programming Introduction to Python Varibles and Datatypes in Python Operators in Python Typecasting in Python Input and Output in Python If Else in Python Loops in Python Break, Continue and Pass in Python Python practice section 1 Lists in Python Tuple in Python

Tuple in Python

Jump to specific section:
  1. Create Tuple
  2. Iterate Over Tuple
  3. Tuple Indexing
  4. Tuple Slicing
  5. Immutable
  6. Tuple Methods
  7. Other Functions
Tuple in Python is a data type that is used to store multiple values or items in a single variable like a list. We know the list is mutable(which means its items can be changed) but the tuple is immutable. Once we store a value in a tuple, we can not change it.

How to create Tuple?

We create Tuple using round brackets. We put comma-separated values in round brackets to form a tuple. But when we create a tuple of size one, we put a trailing comma. Consider the below code for better understanding.

# tuple of size more than 1
tuple1 = (1,2,3,4,5)
print(f"datatype of tuple1 = {type(tuple1)}")
# these strings are called fstrings in python
print(f"Value of tuple1 = {tuple1}")
print()

# tuple of size 0
tuple2 = ()
print(f"datatype of tuple1 = {type(tuple2)}")
print(f"Value of tuple1 = {tuple2}")
print()

# tuple of size 1
# note trailing comma 
tuple3 = (1,)
print(f"datatype of tuple1 = {type(tuple3)}")
print(f"Value of tuple1 = {tuple3}")
print()
            
Output
datatype of tuple1 = <class 'tuple'>
Value of tuple1 = (1, 2, 3, 4, 5)

datatype of tuple1 = <class 'tuple'>
Value of tuple1 = ()

datatype of tuple1 = <class 'tuple'>
Value of tuple1 = (1,)
                    

What if we don't give a trailing comma at the end when we create a tuple of size one? Let's understand with help of code.

# with trailing comma
tuple1 = (1,)
print(type(tuple1))

# without trailing comma
tuple2 = (1)
print(type(tuple2))

if(1 == tuple2):
    print("It is considered as int")   
Output:
<class 'tuple'>
<class 'int'>
It is considered as int

We can iterate over a tuple using for loop in the following way:

tuple1 = ("Rapid","Coders","Akshay")

for i in tuple1:
    print(i)
Output:
Rapid
Coders
Akshay

Tuple indexing

Same as the list we can get value by index in a tuple. Let us see this in code:

tuple1 = ("Rapid","Coders","Akshay")
# Every value of tuple can be accessed by its index
# tuple1  = ["Rapid","Coders","Akshay"]
# index = [0,1,2]

# acessing value present at 0 index
print(tuple1[0])
print(tuple1[2])

print()
print("Now lets learn about negative index: ")
print("index of last element is -1")
print("index of last second element is -2 and so on")
print()
print(tuple1[-1])
Output:
Rapid
Akshay

Now lets learn about negative index: 
index of last element is -1
index of last second element is -2 and so on

Akshay

Tuple can be sliced, slicing means getting a part of a sequence.

Syntax:
    tuple_variable[Start_index : Stop_index]
Slice operations return a new tuple containing the requested elements. In this new tuple, we have all elements from start index to stop index, start index is included but stop index is not included.
Example:

tuple1 = ("Rapid","Coders","Akshay")

print("slicing from index 0 to 1",end=" = ")
print(tuple1[0:2])
print("slicing from index 1 to 2",end=" = ")
print(tuple1[1:2])
Output:
slicing from index 0 to 1 = ('Rapid', 'Coders')
slicing from index 1 to 2 = ('Coders',)

Tuple is immutable:

Immutable means the values of a tuple can not be changed. We need to create a new tuple if we need to store different value. Consider the following code for better understanding:

tuple1 = (1,2,3,4,5)

# lets try to change value of index 0
tuple1[0] = 123
Output:
Traceback (most recent call last):
File "", line 1, in 
TypeError: 'tuple' object does not support item assignment

Tuple methods:

  1. tuple.index(value): It returns the position of the first occurrence of "value" in the tuple. If "value" is not present then we will get an error.
  2. tuple.count(value):It counts and returns the number of times "value" is present in the tuple.

Other functions that can be used are:

  1. len(tuple): This will return number of elements present in tuple.
  2. max(tuple): This will return maximum value present in tuple.
  3. min(tuple): This will return minimum value present in tuple.
Consider following code:

tuple1 = (11,12,15,14,9,14)

# tuple methods
print("First occurrence of 14 is at index = ",end="")
print(tuple1.index(14))

print("Count of 14 = ",end="")
print(tuple1.count(14))
print()

# Other functions that can be used are
print("length of tuple = ",end="")
print(len(tuple1))
print("max of tuple = ",end="")
print(max(tuple1))
print("min of tuple = ",end="")
print(min(tuple1))
Output:
First occurrence of 14 is at index = 3
Count of 14 = 2

length of tuple = 6
max of tuple = 15
min of tuple = 9