Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Python Tuples

Dr. Harish D. Gadade


Tuples in Python
● Create a Tuple

● Accessing Tuple Elements

○ Indexing

○ Negative Indexing

● Tuple Slicing in Python

● Update a Tuples

● Deleting an Element from Tuple


2
Prof. Harish D. Gadade, COEP Technological University, Pune
Creating Tuple
● A tuple is an inbuilt data type in python.

● Tuples are used to store multiple items in a single variable.

● Tuple is one of 4 built-in data types in Python used to store


collections of data, the other 3 are List, Set, and Dictionary, all
with different qualities and usage.
● A tuple is a collection which is ordered and unchangeable.

T1=(10,20,30,40)
T2=(“Ahaan”, 10,20,”Vihaan”)
print(T1)
print(T2) 3
Prof. Harish D. Gadade, COEP Technological University, Pune
Accessing Tuple Elements
● You can access tuple items by referring to the index number, inside
square brackets:

T=(“Ahaan”, 10,20,”Vihaan”)
print(T[0])
print(T[2])
print(T[3])
print(T[-1])
● Above example shows indexing with negative indexing

4
Prof. Harish D. Gadade, COEP Technological University, Pune
Tuple Slicing
● You 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.

T=(“Ahaan”, 10,20,”Vihaan”)
print(T[:])
print(T[:4])
print(T[2:])
print)T[1:3])
print(T[:-1])
print(T[-3:-1])
5
Prof. Harish D. Gadade, COEP Technological University, Pune
Adding Element to Tuple
● Since tuples are immutable, they do not have a build-in append()
method, but there are other ways to add items to a tuple
○ Convert into a list: We can convert tuple into a list, add
item(s), and convert it back into a tuple.
○ Add tuple to a tuple: We can add tuples to tuples, so we you want
to add one item, (or many), create a new tuple with the item(s),
and add it to the existing tuple:

x = ("Ahaan",10,20,"Vihaan") x = ("Ahaan",10,20,"Vihaan")
y = list(x) y = (“Ayaan”)
y.append("Ayaan") x=x+y
x = tuple(y) print(x)

6
Prof. Harish D. Gadade, COEP Technological University, Pune
Deleting Element from Tuple
● Tuples are unchangeable, so you cannot remove items from it, but you
can use previous method for changing and adding tuple items:
○ Convert into a list: We can convert tuple into a list, add
item(s), and convert it back into a tuple.

x = ("Ahaan",10,20,"Vihaan")
y = list(x)
y.remove(“Vihaan”)
x = tuple(y)

7
Prof. Harish D. Gadade, COEP Technological University, Pune
Inbuilt Functions for Tuple
● Python provides various inbuilt functions that can be used with tuples

Inbuilt Function Meaning / Purpose

len() Returns the number of elements in a tuple

max() Returns the element with the greatest value

min() Returns the element with the smallest value

sum() Returns the sum of all the elements of a tuple

index() Return the index of element x

count() Returns the number of occurrences of element x


8
Prof. Harish D. Gadade, COEP Technological University, Pune

You might also like