0% found this document useful (0 votes)
16 views10 pages

Tuple Updated

The document contains a series of Python programming exercises focused on tuples, including creating Fibonacci sequences, calculating maximum and minimum values, and handling nested tuples. It also covers user input for creating tuples, calculating averages and means, and checking for specific conditions within tuples. Each exercise includes a code snippet and example output to demonstrate the expected results.

Uploaded by

felixyt428
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

Tuple Updated

The document contains a series of Python programming exercises focused on tuples, including creating Fibonacci sequences, calculating maximum and minimum values, and handling nested tuples. It also covers user input for creating tuples, calculating averages and means, and checking for specific conditions within tuples. Each exercise includes a code snippet and example output to demonstrate the expected results.

Uploaded by

felixyt428
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

TUPLE

Q. Write python that create a tuple storing first 9 term of


Fibonacci series.
Answer :-
fib = (0,1,1)
for i in range(6):
fib = fib + ( fib [ i +1] + fib [ i + 2 ] ,)
print(fib)

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
>>>

Enter the list = [12,78,69,52,23,32,41]


Enter the index number = 7
Index out of range
>>>

(b)
Enter Fibonacci Term =55
Fibonacci Series :- (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55)

This is 11th term of Fibonacci


>>>

Enter Fibonacci Term =30


Not Exist in Fibonacci Series
>>>
Q. Write a program to input n numbers from the user.
Store these numbers in a tuple. Print the maximum and
minimum number from this tuple.
Answer =

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 :-

Enter a roll number :- 1


Enter name :-Path
Enter marks :-98
Do you want to quit enter yes =no
Enter a roll number :- 5
Enter name :-Walla
Enter marks :-95
Do you want to quit enter yes =no.
Enter a roll number :- 7
Enter name :-Portal
Enter marks :-80
Do you want to quit enter yes =no
Enter a roll number :- 8
Enter name :-Express
Enter marks :-70
Do you want to quit enter yes =yes
((1, 'Path', '98'), (5, 'Walla', '95'), (7, 'Portal', '80'), (8, 'Express', '70'))
>>>
Q. Write a program that interactively create a nested tuple
to store the marks in three subjects for five 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()
mark = (mark1 , mark2 , mark3)
total= total + (mark,)
print("Final Tuple= ",total)
Output :-

Enter Marks of Student no. 1


Enter the marks of first subject = 95
Enter the marks of second subject = 86

3
Enter the marks of third subject = 42

Enter Marks of Student no. 2


Enter the marks of first subject = 64
Enter the marks of second subject = 29
Enter the marks of third subject = 34

Enter Marks of Student no. 3


Enter the marks of first subject = 85
Enter the marks of second subject = 97
Enter the marks of third subject = 45

Enter Marks of Student no. 4


Enter the marks of first subject = 23
Enter the marks of second subject = 45
Enter the marks of third subject = 13

Enter Marks of Student no. 5


Enter the marks of first subject = 96
Enter the marks of second subject = 58
Enter the marks of third subject = 94

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

Enter Marks of Student no. 2


Enter the marks of first subject = 98
Enter the marks of second subject = 56
Enter the marks of third subject = 25

Total marks of student no. 2 :- 179


Average marks of student no. 2 :- 59.666666666666664

Enter Marks of Student no. 3


Enter the marks of first subject = 98
Enter the marks of second subject = 99
Enter the marks of third subject = 91

Total marks of student no. 3 :- 288


Average marks of student no. 3 :- 96.0

Enter Marks of Student no. 4


Enter the marks of first subject = 65
Enter the marks of second subject = 85
Enter the marks of third subject = 74

Total marks of student no. 4 :- 224


Average marks of student no. 4 :- 74.66666666666667

Enter Marks of Student no. 5


Enter the marks of first subject = 78
Enter the marks of second subject = 65
Enter the marks of third subject = 96

Total marks of student no. 5 :- 239


Average marks of student no. 5 :- 79.66666666666667

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)
>>>

Q. Write a program as per following specification:


"Return the length of the shortest string in the tuple of a
strings str_tuple. Precondition: the tuple will contain at
least one element."
Answer :-

tup = eval(input("Enter the string tuple = "))


short = tup [ 0 ]
for i in tup :
if len(i) < len( short ) :
short = i
print("Smallest string in tuple = ",short)

Output :-
Enter the string tuple = ("path","walla","portal","express","computer","python")
Smallest string in tuple = path
>>>

Enter the string tuple = ("Python","C+++","Java","CSS")


Smallest string in tuple = CSS
>>>
Q. Create the following tuple using a for loop :
(i) A tuple containing the squares of the integer 1 though 50.
(ii) The tuple (“a”, “bb”, “ccc”….)that ends with 26 copies of the letter z.
Answer :-
(i)
tup = ()
for i in range(1,51):
tup = tup + ( i**2,)
print(tup)

(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
>>>

Enter first tuple = (1,2,3,4,5,6,7,8,9)

7
Enter second tuple = (1,2,3,4,5,6,7,8)
False
>>>

Enter first tuple = (1,3,5,7)


Enter second tuple = (2,4,6,8)
False
>>>
Q. Computing mean. Computing the mean of values
stores in a tuple is relatively simple. The mean is the sum
of the values divided by the number of values in the tuple.
mean = Sum of frequency /no.of frequency
Write a program that calculate and display the mean of a
tuple with numeric element .
Answer :-
a = eval (input ("Enter the numeric tuple ="))
b=0
for i in range (len(a)):
b = b +a[i]
print('mean of tuple =',b/len(a))
Output :-

Enter the numeric tuple = (1,2,3,4,5,6,7,8,9)


Computing mean of tuple = 5.0
>>>

Enter the numeric tuple = (85,74,26,18,39,45)


Computing mean of tuple = 47.833333333333336
>>>
Q. Write a program to check the mode of a tuple is
actually an element with maximum occurrences.
Answer =
tup = eval(input("Enter a tuple :-"))
count = 0
for i in tup :
if count < tup.count(i):
count = tup.count(i)
for i in tup :
if count == tup.count(i) :
print("mode = ",i)
break

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
>>>

Enter a tuple :-(1,2,3,4,5,6,7,8,9,10)


Average = 5.5
Mean = 5.5
>>>
Q. 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
mean of individual element of tuple tup1 and then displays
the mean of these computed means.
Answer :-
a = ((1,2),(3,4.15,5.15),(7,8,12,15))
sum = 0
mean = 0
me= 0
for i in range (len(a)):
sum = 0
for j in range (len(a[i])):
sum = sum+a [i][j]
mean = sum /(j +1)
print ("Mean element ", i + 1 , ":",mean)
me += mean
print ("Mean of means : ",me /3)

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.

Write a program to compute the standard deviation for


the numeric samples collected in a tuple namely
samples.
Answer =
import math
tup = eval (input("Enter the numeric tuple "))
n = len (tup)
sum = 0
lst = []
for i in range (n):
sum = sum + tup[i]
mean = sum /n
print ("mean ",mean)
for i in range (n):
lst.insert(i,tup[i]-mean)
print ("List ",lst)
square = []
for i in range (n):
square.insert(i,lst[i]*lst[i])
sum = sum + lst [i] * lst[i]
print ("Square of each element ",square)
s = math.sqrt (sum/(n-1))
print ("Standard deviation :",s)

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

You might also like