Tuple Updated
Tuple Updated
Output :-
(0, 1, 1, 2, 3, 5, 8, 13, 21)
>>>
Q.
(a). Write a program that receive the index and return the
corresponding value.
(b). Write a program that receives a Fibonacci term. And
return and returns a number telling which term it is .
Answer :-
(a).
a = eval (input('Enter the list ='))
b = int (input ("Enter the index number ="))
if b< len(a):
print ("value is =", a[b])
else :
print ("index out of range")
(b).
term= int (input ("Enter Fibonacci Term ="))
fib = (0,1,1)
for i in range(term):
fib = fib + ( fib [ i + 1] + fib [ i + 2 ],)
for j in range(len(fib)):
if term == fib[ j ] :
break
if j == len(fib)-1:
print("Not Exist in Fibonacci Series ")
else :
print("Fibonacci Series :- ", fib [ 0 :j +1] )
print()
print("This is ",j + 1,"th term of Fibonacci ")
Output :-
(a)
Enter the list = [1,2,3,4,5,6,7,8,9]
Enter the index number = 8
Value is = 9
1
>>>
(b)
Enter Fibonacci Term =55
Fibonacci Series :- (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55)
tup= ()
while True :
num = int(input("Enter a number :- "))
tup += (num,)
user = input("Do you want to quit enter yes =")
if user == "yes":
print(tup)
print("Max :-",max( tup ))
print("Min :-",min( tup ))
break
Output :-
Enter a number :- 9
Do you want to quit enter yes =no
Enter a number :- 7
Do you want to quit enter yes =no
Enter a number :- 6
Do you want to quit enter yes =no
Enter a number :- 2
Do you want to quit enter yes =no
Enter a number :- 8
Do you want to quit enter yes =yes
(9, 7, 6, 2, 8)
Max :- 9
Min :- 2
>>>
Q. Write a program to create a nested tuple to store roll
number, name and marks of students.
Answer:-
2
tup= ()
while True :
roll = int(input("Enter a roll number :- "))
name = input("Enter name :-")
mark = input("Enter marks :-")
tup += ( (roll,name,mark ),)
user = input("Do you want to quit enter yes =")
if user == "yes":
print(tup)
break
Output :-
total = ()
for i in range(5):
print("Enter Marks of Student no.", i+1 )
mark = ()
mark1 = int(input("Enter the marks of first subject = "))
mark2 = int(input("Enter the marks of second subject = "))
mark3 = int(input("Enter the marks of third subject = "))
print()
mark = (mark1 , mark2 , mark3)
total= total + (mark,)
print("Final Tuple= ",total)
Output :-
3
Enter the marks of third subject = 42
Final Tuple= ((95, 86, 42), (64, 29, 34), (85, 97, 45), (23, 45, 13), (96, 58, 94))
>>>
Q. In the program created in previous question , add a
function that computes total marks and average marks
obtain by each student .
Answer :-
total = ()
for i in range(5):
print("Enter Marks of Student no.", i+1 )
mark = ()
mark1 = int(input("Enter the marks of first subject = "))
mark2 = int(input("Enter the marks of second subject = "))
mark3 = int(input("Enter the marks of third subject = "))
print()
print("Total marks of student no." ,i+1,":-", mark1 + mark2 + mark3)
print("Average marks of student no." ,i+1,":-", (mark1 + mark2 + mark3 )/3)
mark = (mark1 , mark2 , mark3)
total= total + (mark,)
print()
print("Final Tuple= ",total)
Output :-
Enter Marks of Student no. 1
Enter the marks of first subject = 58
Enter the marks of second subject = 57
Enter the marks of third subject = 65
4
Total marks of student no. 1 :- 180
Average marks of student no. 1 :- 60.0
Final Tuple= ((58, 57, 65), (98, 56, 25), (98, 99, 91), (65, 85, 74), (78, 65, 96))
>>>
Q. Write a program that inputs two tuples and creates a
third, that contains all elements of the first followed by all
elements of the second.
Answer =
tup1 = eval(input("Enter First tuple :-"))
tup2 = eval(input("Enter second tuple :-"))
tup3 = tup1 + tup2
print(tup3)
Output :-
5
Enter First tuple :-(1,3,5,7,9)
Enter second tuple :-(0,2,4,6,8)
(1, 3, 5, 7, 9, 0, 2, 4, 6, 8)
>>>
Output :-
Enter the string tuple = ("path","walla","portal","express","computer","python")
Smallest string in tuple = path
>>>
(ii)
tup = ()
for i in range(1 , 27):
tup = tup + ( chr(i + 96 )* i ,)
print(tup)
Output :-
(i)
(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484,
529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521,
1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500)
>>>
6
(ii)
('a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll',
'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn', 'ooooooooooooooo', 'pppppppppppppppp',
'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss', 'tttttttttttttttttttt',
'uuuuuuuuuuuuuuuuuuuuu', 'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww',
'xxxxxxxxxxxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz')
>>>
Q. Given a tuple pair ((2,5),(4,2),(9,8),(12,8)),count the
number of pair (a,b) such that a and b are even .
Answer :-
tup = ((2,5),(4,2),(9,8),(12,8))
count = 0
for i in range (len(tup)):
if tup [i][0] % 2 == 0 and tup[i][1] % 2 == 0:
count += 1
print("The number of pair (a,b) such that a and b are even = ",count)
Output :-
The number of pair (a,b) such that a and b are even = 2
>>>
Q. Write a program that input two tuple seq_a and seq_b
and print True if every element in seq_a is also an element
of seq_b , else print False.
Answer :-
tup1 = eval(input("Enter first tuple = "))
tup2 = eval(input("Enter second tuple = "))
count = 0
if len(tup1)==len(tup2):
for i in range(len(tup1)) :
if tup1[i] == tup2 [ i ] :
continue
else :
count += 1
if count == 0 :
print("True")
else :
print("False")
else :
print("False")
Output :-
Enter first tuple = (1,2,3,4,5,6,7,8,9)
Enter second tuple = (1,2,3,4,5,6,7,8,9)
True
>>>
7
Enter second tuple = (1,2,3,4,5,6,7,8)
False
>>>
Output :-
Enter a tuple :-(1,5,4,8,7,5,4,3,8,0,5,4,2)
mode = 5
>>>>
8
Q. Write a program to calculate the average of a tuple
element by calculating its sum and dividing it with the
count of the elements. Then compare it with the mean
obtained using mean() of statistics module.
Answer :-
import statistics
tup = eval(input("Enter a tuple :-"))
sum = sum(tup)
print("Average =", sum / len( tup ))
print("Mean =", statistics.mean( tup ) )
Output:-
Enter a tuple :-(3,7,8,12,15,19,21,25)
Average = 13.75
Mean = 13.75
>>>
Output :-
Mean element 1 : 1.5
Mean element 2 : 4.1000000000000005
Mean element 3 : 10.5
Mean of means : 5.366666666666667
>>>
9
Q. Standard deviation. The standard deviation can be
done a few ways but we will use the formula shown below.
This computes a deviation measurement as the square of
the difference between each element and the mean.
Output :-
Enter the numeric tuple (5,9,15,16,19,28,36,38)
mean 20.75
List [-15.75, -11.75, -5.75, -4.75, -1.75, 7.25, 15.25, 17.25]
Square of each element [248.0625, 138.0625, 33.0625, 22.5625, 3.0625, 52.5625, 232.5625,
297.5625]
Standard deviation : 13.057564857200596
>>>
10