2. Tuple in Python
Tuple is an ordered sequence of items
same as list. The only difference is that
tuple are immutable.
Tuples once created cannot be modified It
is defined within parentheses () where
items are separated by commas
The values of tuple is either string or
numeric.
For example:(“t”, ”u”, ”p”, ”l”, ”e”) #String
(1,2,3,4) #Numeric
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
3. Creating a Tuple
Creating w tuple with parentheses.
A tuple is created by placing all the items
(elements) inside parentheses ().
Example :
>>> T = ('p','y','t','h','o','n')
>>> type(T)
<class 'tuple'>
Creating tuple without parentheses.
A tuple can also be created without using parentheses.
This is known as tuple packing.
Example: >>> my_T = 1,2,3,
>>> type(T)
<class 'tuple'>
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
4. Access Tuple Elements
There are various ways in which we can
access the elements of a tuple.
Indexing:
Indexing is way to access the element of
tuple.
Indexing are two types:-
Positive Indexing
We can use the index operator [] to access
an item in a tuple where the index starts
from 0.
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
5. Cont….
Negative Indexing:
Python also allows negative indexing for
accessing elements of tuple.
The index of -1 refers to the last item, -2
to the second last item and so on.
Indexing of Tuple -
-6 -5 -4 -3 -2 -1
0 1 2 3 4 5
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
P Y T H O N
6. Program to access tuple
element by indexing
>>> my_tuple = ('p','y','t','h','o','n')
>>> print(my_tuple[-3])
h
>>> print(my_tuple[-5])
y
>>> print(my_tuple[2])
t
>>> print(my_tuple[4])
o
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
7. Slicing:
We can access a range of items in a tuple by using
the slicing operator – colon”:”.
Syntax of slicing:
T (start : stop : step)
Slicing creates list of elements falling between indexes
start & stop , not including stop (upto indexing before
stop).
Slicing include positive & negative indexing-
-7 -6 -5 -4 -3 -2 -1
0 1 2 3 4 5 6
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
P A T I D A R
8. Program related to
slicing:
my_tuple = ("Gori","Radhika","Raj","Pari")
>>> my_tuple[1:3]
('Radhika', 'Raj')
>>> my_tuple1
=(10,15,20,25,30,35,40,45,50,55,60,65)
>>> my_tuple1[3:10]
(25, 30, 35, 40, 45, 50, 55)
>>> my_tuple1[2:9:2]
(20, 30, 40, 50)
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
9. Changing a Tuple:
Tuples are immutable that means we can not
be changed once it has been assigned.
We can change tuple element by converting
tuple into list .
We can change element of list by their
indexing .
Program to change element of tuple:
>>> my_tuple = (10,50,40,20,3,5,30,9)
>>> L = list(my_tuple)
>>> L[5] = 90
>>> my_tuple = tuple(List)
>>> print(my_tuple)
(10, 50, 40, 20, 3, 90, 30, 9)
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
10. Use of operates in tuples:
Addition operates: It is used to add two or
more tuples, to create new tuple.
Program to add a tuple-
>>> T1 = (12,30,50,16,15)
>>> T2 = (20,10,2,5,4,6)
>>> T3 = (3,4,6,8,7)
>>> T = T1+T2+T3
>>> print(T)
(12, 30, 50, 16, 15, 20, 10, 2, 5, 4, 6, 3, 4, 6,
8, 7)
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
11. Cont….
Multiplication operator:
Multiplication operator to replicate a list
specified number of times .
Program to complete tuple:
>>> my_tuple = (1,2,3,4,5)
>>> my_tuple1 = my_tuple*3
>>> print(my_tuple1)
(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
12. Cont….
Membership operators:
Membership operators are-
in and not in.
It is used to check the element ‘in’ and ‘not in’ in tuple .
Program of relationship operators:
>>> my_tuple1 = ("Raj","Pari","Raju")
>>> "Pari"in(my_tuple1)
True
>>> "Mohit"in(my_tuple1)
False
>>> my_tuple = (10,20,30,40,50)
>>> 30in(my_tuple)
True
>>> 70in(my_tuple)
False
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
13. >>> my_tuple = (2,5,8,10,1)
>>> 10 not in(my_tuple)
False
>>> my_tuple = (2,5,8,10,1)
>>> 4 not in(my_tuple)
True
>>> my_tuple = ("Mohit","Rohit","Parinika")
>>> "Parinika" not in(my_tuple)
False
>>> my_tuple = ("Mohit","Rohit","Parinika")
>>> "Meet" not in(my_tuple)
True
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
Cont…
14. Deleting a Tuple
We cannot change the elements in a
tuple. That means , we cannot delete or
remove items from a tuple.
But , in tuple we can delete the elements
by using the keyword del.
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
15. Tuple Programs-
1.
>>> T = ("tuple",False,3.2,1)
>>> print(T)
('tuple', False, 3.2, 1)
2.
>>> x = ("apple","banana","cherry")
>>> y = list(x)
>>> y[1] = "kiwi"
>>> x = tuple(y)
>>> print(x)
('apple', 'kiwi', 'cherry')
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
16. 3.
>>> tuple = ("Check")*3
>>> print(tuple)
CheckCheckCheck
4.
>>> P = (11,14,15,13,2,8,9)
>>> q = (2,5,7,9,8,3)
>>> r = ('a','e','i','o','u')
>>> H = (P+q+r)
>>> print(H)
(11, 14, 15, 13, 2, 8, 9, 2, 5, 7, 9, 8, 3, 'a', 'e', 'i', 'o',
'u')
MAA UMIYA PATIDAR GIRLS' HR. SEC. SCHOOL
Cont…