TUPLES Notes
TUPLES Notes
immutable
What is Tuple?
Are sequence that are used to store a tuple of
values of any type
Tuples a re im m uta ble i.e. you c a n n ot
c h a nge the elements of tuple in p la ce .
Python will create a fresh tuple when we m a ke
ch a n ge s to a n element of tuple.
Creating a n d accessing
tuples
Tuples a re create d just like list exce p t by
parenthesis “ ( ) ” in p l a c e of square bracket “[]”
Examples of tuple :
()
(1,2,3)
(2,2.5,4,1.2)
(“ a‟,1,‟b‟,2,‟c‟,3)
(“ red”,”green”,”blue”)
Creating tuples
T = () # empty tuple
T= (value1, value2, value3,….)
1. Empty Tuple
T = () Or
T = tuple()
Creating tuples
2. Single element Tuple
>>> T= (20)
>>> T
20
>>> T= 5,
>>> T
(5,)
>>> T= (100,)
>>> T
(100,)
Creating tuples
3. Creating long tuples
roots = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
4. Nested tuples
>>> T1 = (10,20,30,(40,50,60),100)
>>>len(T1) #5
>>>T1[1] #20
>>>T1[3][1] #50
Creating tuples from existing sequence
T=tuple(sequence)
>>>T=tuple('python')
>>> T
('p', 'y', 't', 'h', 'o', 'n')
>>> items=[100,200,300,400]
>>>T2=tuple(items)
>>> T2
(100, 200, 300, 400)
>>>t1 =tuple(input('enterelements'))
enter elem entsabc de
>>> t1
('a', 'b', 'c', 'd', 'e')
Using eval() while creating tuple
>>>mytuple=eval(input("enter tuple elements"))
enter tuple elements(10,'ravi',10.5)
>>> mytuple (10, 'ravi', 10.5)
Accessing Tuple
elements
Similarity withstrings:
Just like string, every individual elements of tuples are
accessed from their index position which is from 0 to
length-1 in forward indexing a n d from -1 to – length in
backward indexing.
For example
Fruits =(“mango”,”apple”,”guaua”,”pomegranate”,”cherry”)
>>> L1 = [10,20,30]
>>> T1 = (100,200,300)
>>> L1[1]=200 # VALID
>>>T1[1]= 150 #INVALID coz tuples areimmutable
Traversing tuple
We can use “for” loop to access every element of tuple
qualifications=("B.A.","M.A.","B.Sc","M.Sc","MCA","M.Com","B.Tech")
for q in qualifications:
print(q)
Or
qualifications=("B.A.","M.A.","B.Sc","M.Sc","MCA","M.Com","B.Tech")
for i in range(len(qualifications)):
print("Index :“, i, “ “, qualifications[i])
Tuple operations
1. Joining Tuple
>>> t1=(10,20,30)
>>> t2=('a','b','c')
>>> t3 = t1 + t2
>>> t3
(10, 20, 30, 'a', 'b', 'c')
Note: you c a n a d d tuple with only another tuple a n d not
with int, complex number, string or list
>>>t1 +20 #Error
Ifyou want to add a tuple with another tuple with one value
only and if you write statement as:
>>>t1 +(20) #Error,because (20)will be treated asnumber
Tuple operations
To add single value tuple just add comma(,) after the value
as:
>>> t1 = (10,20,30)
>>> t1 + (50,)
(10,20,30,50)
Replicating Tuple:
>>> t1=("do","it")
>>> t1*3
('do', 'it', 'do', 'it', 'do', 'it')
Slicing Tuples
T[start : end] #all values between index start to end –1
data=(10,20,30,1,7,9,100,51,75,80
)
data2 = data[4:-4]
print(data2)
print(data[1:6])
print(data[4:-2])
print(data[-40:4])
print(data[::-1])
print(data[::-2])
print(data[2:10:2])
Slicing Tuples
T[start : end] #all values between index start to end –1
data=(10,20,30,1,7,9,100,51,75,80) Output
data2 = data[4:-4] (7, 9)
print(data2) (20, 30, 1, 7, 9)
(7, 9, 100, 51)
print(data[1:6]) (10, 20, 30, 1)
print(data[4:-2]) (80, 75, 51, 100, 9, 7, 1, 30,
print(data[-40:4]) 20, 10)
(80, 51, 9, 1, 20)
print(data[::-1]) (30, 7, 100, 75)
print(data[::-2])
print(data[2:10:2])
Slicing Tuples
>>> tp1 = (11,12,15,20,8,9,10)
>>> seq1 = tp1[::2]
>>> seq1 = tp1[5::2]
>>> tp1[2:5]*3
(15, 20, 8, 15, 20, 8, 15, 20, 8)
>>> tp1[2:5] + (500,1000)
(15,20,8,500,1000)
Comparing tuples
>>> a=(10,20)
>>> b=(10,20)
>>> c=(20,10)
>>> a==b
True
>>> a ==c
False
>>>
d=(20.0,10.0)
>>> c==d
True
>>>
a<c
True
Unpacking tuples
Creating a tuple from a set of values is called packing a n d its
reverse i.e. creating individual values from tuple‟s elements is
called unpacking.
Unpacking is done by using following
syntax: var1, var2, var3, … =
tuple_Object
Example:
>>> t1 = (100,200,300,400)
>>> a,b, c , d = t1
>>>
a
100
>>> b
200
>>> c
T300
Deleting tuples
The del statement of python is used to delete elements a n d
objects but as you know that tuples are immutable, which also
m eans that individual elements of tuples cannot b e deleted.
For exam ple
del t1[2] #Error,coz elements of tuple cannot be deleted
>>>t1 = ( 10,20,30)
>>> print(t1)
(10,20,30)
>>> del t1
>>> print(t1) # Error t1 is not defined
Tuple functions a n d methods
1. len() : returns number of elements in the tuple
>>> book = („B001‟,‟Let Us Python‟,‟DP‟,500)
>>>
len(book) 4
2. max() : returns element from tuple having maximum value
>>> salary=(1000,1500,800,700,1200)
>>> max(salary)
1500
>>> fruits=("mango","pine apple","apple","carrot")
>>> max(fruits)
'pine apple'
Note: max() function will return maximum value only if all the elements
in tuple is of same type. If elements are of different type then python
will raise a n exception.
>>> t1 = (10,20,30,(40,50),90)
>>> max(t1) # Error
Tuple functions a n d methods
3. min() :returns element from tuple having minimum value
>>> salary=(1000,1500,800,700,1200)
>>>
min(salary)
700
>>> fruits=("mango","pine apple","apple","carrot")
>>> min(fruits)
'apple„
Note: min() function will return minimum value only if all the elements
in tuple is of same type. If elements are of different type then python
will raise a n exception.
>>> t1 = (10,20,30,(40,50),90)
>>> min(t1) # Error
4. index() : it return index value of given element in the list, if element
not present
) it raises ValueError exception
Tuple functions a n d methods
5.count() : it return the count of any element in the tuple i.e.
how many times the given element is in the tupleg. If an
element not in the tuple it return 0.
>>> val=(10,20,30,20,10,60,80,20)
>>>
val.count(20) 3
>>>
val.count(80) 1
>>> val.count(100)
0
6.tuple(): this method is actually a constructor used to create tuples
from different type of values.
Creating empty tuple
tup = tuple()
Tuple functions a n d methods
Creating tuple from string
tup = tuple(“quick brown fox”)
Creating a tuple from a list
tup = tuple([1,20,40])
Creating a tuple from keys of dictionary
>>> tup = tuple({1:”One”,2:”Two”})
>>> tup # (1,2)