tup = (1,2,3,4,5,6,7,8,9,0,11,22,33,44)
##the number of elements in the tuple.
print ("number of elements :", len (tup))
##the minimum element in the tuple.
print ("minimum value :" ,min (tup))
##the maximum element in the tuple.
print ("maximum value :" ,max (tup))
##sum,average,total of tuple.
print ("sum :" ,sum (tup),"Average :" ,sum (tup)/len (tup))
## using list because tuple is immutable
##tuple in reverse.
lit = list (tup)
lit = reversed (lit)
print ("tuple in reverse :" ,tuple (lit))
##tuple elements along with indexes.
for i in range (len (tup)):
print (str (i)+"--" +str (tup[i]))
## it is impossible to edit a tuple.
##adding element to tuple at last.
a = (0,1,2,3,4,5,6,7,8,9)
a = list (a)
a.append (100)
print (tuple (a))
##adding element at particular index.
a.insert(1,23)
print (tuple (a))
##Deleting tuple element.
a.remove (6)
print (tuple (a))
##Changing a tuple element.
a.sort ()
print (tuple (a))
T = (11,22,33,44,55,66,77,88,99)
new1 = []
new2 = []
for i in T:
if i%2 == 0:
new1.append (i)## it stores even values
else :
new2.append (i)## it stores odd values
print ("Even tuple :" ,tuple (new1),"Odd tuple :" ,tuple (new2))
a = (1,2,3,4,"hello" ,"good" ,"morning" ,"friends" ,1.23,3.14,55.43,(1j),(54j),[1.1,1.2,1.3],["hi" ,"hello" ],[1,2,3],("a" ,"b" ,"c" ),(101,102))
n = f = c = s = l = t = 0
for i in a:
if type (i) == type (1):
n = n+1
if type (i) == type (1.5):
f = f+1
if type (i) == type ((1j)):
c = c+1
if type (i) == type ("1" ):
s = s+1
if type (i) == type ([]):
l = l+1
if type (i) == type (()):
t = t+1
print ("Integers :" ,n,"\nFloat :" ,f,"\nComplex :" ,c,"\nstrings :" ,s,"\nLists :" ,l,"\nTuples :" ,t)
a = (1,2,3,4,"hello" ,"good" ,"morning" ,"friends" ,1.23,3.14,55.43,(1j),(54j),[1.1,1.2,1.3],["hi" ,"hello" ],[1,2,3],("a" ,"b" ,"c" ),(101,102))
n,f,c,s,l,t = [],[],[],[],[],[]
for i in a:
if type (i) == type (1):
n.append (i)
if type (i) == type (1.5):
f.append (i)
if type (i) == type ((1j)):
c.append (i)
if type (i) == type ("1" ):
s.append (i)
if type (i) == type ([]):
l.append (i)
if type (i) == type (()):
t.append (i)
print ("Integers :" ,tuple (n),"\nFloat :" ,tuple (f),"\nComplex :" ,tuple (c),"\nstrings :" ,tuple (s),"\nLists :" ,tuple (l),"\nTuples :" ,tuple (t))
a = (1,2,3,4,"hello" ,"good" ,"morning" ,"friends" ,1.23,3.14,55.43,(1j),(54j),[1.1,1.2,1.3],["hi" ,"hello" ],[1,2,3],("a" ,"b" ,"c" ),(101,102))
n = []
for i in a:
if type (i) == type (1) or type (i) == type (1.5) or type (i) == type ("1" ):
n.append (i)
a = tuple (n)
print (a)
a = (1,22,3,4,55,6,77,8,99,0)
b = (11,22,33,44,55,66,77,88,99,100)
inter = []
union = list (a)
for i in a:
if i in b:
inter.append (i)
for j in b:
if j not in a:
union.append (j)
print (inter,union)
tup = (1,1,1,2,2,3,33,4,5,6,6,5,7,8,9,6,7,7,4,5,3,9,10)
new = []
for i in tup:
if i not in new:
new1 = [i for j in range (tup.count (i))]
new1 = tuple (new1)
if new1 not in new:
new.append (new1)
new = tuple (new)
print (new)
a = ((1,2,3,4,0),(11,22,3,44,5),(-111,-23,-345,-4567),(100,100,10),(11,1),(66,99,66,99,66,99),(109,202,106,404),(9,99,-99,99))
for i in range (len (a)):## sort each sub tuple.
new = sorted (a[i])
a = list (a)
a[i] = tuple (new)
a = tuple (a)
print (a)
print ("last elements of each sub tuple." )
for i in a:## last elements of each sub tuple.
print (i[-1:])
print ("the total of all the sub tuples." )
for i in a:## the total of all the sub tuples.
print (sum (i))
tup = ("hello" ,"hello world" ,"print python" ,"welcome" ,"workshop" ,"123@abcd" )
length = len (tup) ## Count the number of words in the tuple.
print (length)
new = ""
for i in tup:## Create a new string with first character in sub tuple.
new = new+str (i[:1])
print (new)
for i in tup:## sub tuples without special characters.
store = []
for j in i:
if chr (65) <= j <= chr (90) or chr (97) <= j <= chr (122) or chr (48) <= j <= chr (57):
store.append (j)
print (tuple (store))
tup = ("1" ,"v" ,"A" ,"4" ,"Y" ,"s" ,"Z" ,"e" ,"5" ,"@" ,"[" ,"&" ,"}" ,"$" ,"6" )
new = []
for i in tup:## new tuple with their ascii values
new.append (ord (i))
print (tuple (new))
spe = []
for j in tup:## new tuple with only special characters.
if chr (65) <= j <= chr (90) or chr (97) <= j <= chr (122) or chr (48) <= j <= chr (57):
pass
else :
spe.append (j)
print (tuple (spe))
tupnew = []
for i in tup:## new tuple with change cases.
if i.islower () == True :
tupnew.append (i.upper ())
elif i.isupper () == True :
tupnew.append (i.lower ())
else :
tupnew.append (i)
print (tuple (tupnew))
Mentor in IT,Dept of IT,RGUKT-SRIKAKULAM
duppada8@rguktsklm.ac.in
Projects
Minutes Of Support
Hard Worker