Contents:
- Introduction
- Creation of Tuple
- Traversal of Tuple
- Operation on Tuple
- concatenation
- repetition
- membership
- Tuple slicing
- mean of values stored in a tuple
- Linear search on a tuple of numbers
- counting the frequency of elements in a tuple
- Deleting element from a tuple
- Function/Methods on Tuple
- Excercise
() #Tuple with no members, empty tuple
(1,2,3) #tuple of integer
(1,2.5,'hello') #Heterogenous tuple i.e. tuple containing multiple data type
('a','b','d') # tuple of string
Empty Tuple:
tu=tuple()
print(tu)
Single element tuple:
tu=(1)
print(tu)
Creating tuple from existing sequences:
tu=tuple([12,13,14,15,16])
print(tu)
tu=tuple("Hello")
print(tu)
val=input("enter a value")
tu=tuple(val)
print(tu)
#Accessing an individual element
vowels=('a','e','i','o','u')
vowels[2]
vowels=('a','e','i','o','u')
for i in vowels:
print(i)
Joining tuple: + operator can be used to join/Concatenate tuple
tp1=(1,2,3,4)
tp2=(5,6,7,8)
tp1+tp2
Replicating Tuple: * operator with an integer can be used to replicate tuple
tp1=(1,2,3,4)
tp1*3
Slicing Tuple: Tuple slicing is similar to List slicing
Syntax:
seq=T[start:stop:step]
#Slicing a tuple
tp1=(1,2,3,4,5,6,7)
tp1[2:5]
#Striding a tuple
tp1=(1,2,3,4,5,6,7)
tp1[2:5:2]
Note:
Creating a tuple from a set of values is called packing and its reverse i.e. creating individual values from a tuple's elements is called unpacking.Syntax:
variable1,variable2,variable3,..= t
a,b,c=(10,12,13) #TUPLE UNPACKING
print(a)
print(b)
print(c)
Membership operation:
in and not in
can be used to check for membership in a tupletp1=(1)
tp2=(1,2,3,4,5)
print(tp1 in tp2)
print(tp1 not in tp2)
Mean of the elements of a tuple:
tp=(10,20,30,40,50)
s=0
for i in tp:
s=s+i
mean=s/len(tp)
print(mean)
Linear search on a tuple of numbers:
tp=(10,20,30,40,50)
f=int(input("Enter the element to search"))
if f in tp:
print("The element is present")
else:
print("The element is absent")
Counting the frequency of elements in a tuple:
tp=(10,15,10,12,10,11,10)
count=0
elm=10
for i in tp:
if elm==i:
count+=1
print("The number of occurances of ",elm," is ",count)
Deleting element from Tuple: del statement can be used to delete an entire tuple but individual element can not be deleted as tuple are immutable
tp=(10,15,10,12,10,11,10)
del tp
print(tp)
len():
tp=(10,15,10,12,10,11,10)
len(tp)
max():
tp=(10,15,10,12,10,11,10)
max(tp)
min():
tp=(10,15,10,12,10,11,10)
min(tp)
tuple_name.count(item): Returns the count of a members element/object in the tuple
tp=(10,15,10,12,10,11,10)
tp.count(10)
tuple(): converts the sequence(list,string) into tuple
ls=[10,20,30,40,50]
tp=tuple(ls)
print(tp)
str="Computer"
tp2=tuple(str)
print(tp2)
index
tp=(10,15,10,12,10,11,10)
tp.index(10)
sorted():
Sorted() method sorts a Sequence(List,Tuple,String) and always returns a list with the elements in a sorted manner, without modifying the original sequence.Reverse = True argument can be used to sort in Decreasing order
tp=(10,15,10,12,10,11,10)
sorted(tp, reverse=True)
- Write a python program that creates a tuple storing first 9 terms of Fibonacci series.
- Given a tuple pairs=((2,5),(4,2),(9,8),(12,10)), count the number of pairs (a,b) such that both a and b are even
- Mean of Means: Given a nested tuple tup1=((1,2),(3,4.15,5.15),(7,8,12,15)). write a program that display the means of individual elements of tuple tup1 and then display the mean of these compound mean. That is for above tuple, it display as:
Mean element 1: 1.5;
Mean element 2: 4.1
Mean element 3: 10.5
Mean of means 5.366666