Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

TUPLES: CREATING , ACCESSING ,

UPDATING & DELETING TUPLE IN


PYTHON
BY : A.TARUN
ROLL NO :- 22211A0514
TUPLES
◦ A tuple is similar to the list as it also consists of a
number of values separated by commas and
enclosed within parentheses.
CREATING TUPLES

◦ Tuples are immutable in python , which means their elements cannot


be changed after they are created . You can create a tuple using
parentheses ‘()’ or the built-in ‘tuple()’ function.
EXAMPLE CODE
#Creating a tuple using parentheses
my_tuple = (1 , 2 , 3 , 4 , 5 )

#Creating a tuple using tuple() function


my_tuple = tuple([1 , 2 , 3 , 4 , 5])

#Creating a tuple with mixed data types


my_tuple = ( ‘python’, 3.14 , True , [1 , 2 , 3 , 4 , 5])
ACCESSING TUPLE ELEMENTS

◦ You can access individual elements of a tuple using indexing , starting


from 0 for the first element.
EXAMPLE CODE
#Creating a tuple using parentheses
my_tuple = (1 , 2 , 3 , 4 , 5 )

#Acessing tuple elements using indexing

print(my_tuple[0]) #Output : 1
print(my_tuple[2]) #Output : 3
print(my_tuple[-1]) #Output : 5
UPDATING TUPLE

◦ Since tuples are immutable , you cannot update individual elements of


a tuple directly . However , you can create a new tuple with the
desired values using tuple concatenation or other techniques.
EXAMPLE CODE
#Updating tuples using tuple concatenation
# Original tuple
my_tuple = (1, 2, 3, 4, 5)
my_tuple = my_tuple + (6, 7) # Adds (6, 7) to the original tuple
print(my_tuple) # Output: (1, 2, 3, 4, 5, 6, 7)
EXAMPLE CODE
# Updating tuples using tuple packing and unpacking
a=1
b=2
c=3
my_tuple = (a, b, c) # Packing values into a tuple
print(my_tuple) # Output: (1, 2, 3)
a=4 # Updating the value of a
my_tuple = (a, b, c) # Creating a new tuple with updated value of a
print(my_tuple) # Output: (4, 2, 3)
DELETING TUPLE

◦ Tuples are immutable,so you cannot delete individual elements of a


tuple however you can delete entire tuple using ‘del’ keyword.
EXAMPLE CODE
#Deleting a tuple

del my_tuple
THANK YOU

You might also like