Tuple (Python)

TUPLE

1. Write a Python program that creates a tuple storing first 9 terms of Fibonacci series.

fib=(0,)
n=int(input('Please enter a number '))
a,b=0,1
for i in range(n):
 a, b = b, a+b
 fib=fib+(a,)
print(fib)     
2. (a) Write a program that receives the index and returns the corresponding value.

fib=(0,)
n=int(input('Please enter a number '))
a,b=0,1
for i in range(n):
 a, b = b, a+b
 fib=fib+(a,)
print(fib)

readInput=int(input("Enter an index "))
print("Element at index ",readInput," is : ",fib[readInput])
(b) Write a program that receives a Fibonacci term and returns a number telling which term it is for instance, if you pass 3, it returns 5, telling it is 5th term; for 8, it returns 7.

fib=(0,)
n=int(input('Please enter a number '))
a,b=0,1
for i in range(n):
 a, b = b, a+b
 fib=fib+(a,)
print(fib)
readInput=int(input("Enter a fibonacci term: "))
print(fib.index(readInput)+1)
3. Write a program that interactively creates a nested tuple to store the marks in three subjects for five students, i.e., tuple will look somewhat like:
marks=[]
A=[]
B=[]
C=[]
D=[]
E=[]

students=['A','B','C','D','E']
studentList=[A,B,C,D,E]

for x,y in zip(students, studentList):
 print("\nPlease enter Physics marks for student",x," :")
 y.append(int(input()))
 print("Please enter Chemistry marks for student",x," :")
 y.append(int(input()))
 print("Please enter Math marks for student",x," :")
 y.append(int(input()))
 marks.append(tuple(y))

print("Therefore The required tuple is: ")
print(tuple(marks))
4. Write a program that interactively creates a nested tuple to store the marks in three subjects for five students, i.e., tuple will look somewhat like:
Marks ((45, 45, 40), (35, 40, 38), (36, 30, 38), (25, 27, 20), (10, 15, 20)) 
add a function that computes total marks and average marks obtained by each student.
marks=[]
A=[]
B=[]
C=[]
D=[]
E=[]

students=['A','B','C','D','E']
studentList=[A,B,C,D,E]

for x,y in zip(students, studentList):
 print("\nPlease enter Physics marks for student",x," :")
 y.append(int(input()))
 print("Please enter Chemistry marks for student",x," :")
 y.append(int(input()))
 print("Please enter Math marks for student",x," :")
 y.append(int(input()))
 marks.append(tuple(y))

print("Therefore The required tuple is: ")
print(tuple(marks)) 
def totalMarks(i):
 total=sum(i)
 return total

def averageMarks(lst): 
    return float(totalMarks(lst)) / len(lst)

print("\nTotal marks of Student A", totalMarks(A))
print("Total marks of Student B", totalMarks(B))
print("Total marks of Student C", totalMarks(C))
print("Total marks of Student D", totalMarks(D))
print("Total marks of Student E", totalMarks(E))

print("\nAverage marks of Student A", averageMarks(A))
print("Average marks of Student B", averageMarks(B))
print("Average marks of Student C", averageMarks(C))
print("Average marks of Student D", averageMarks(D))
print("Average marks of Student E", averageMarks(E))
5. 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.
_firstTuple_=()
_secondTuple_=()

n=int(input("Enter a number: "))
print('\n')
for x in range(n):
 a=input('Enter tuple one elements: ')
 _firstTuple_=_firstTuple_ + (a,)

n=int(input("\nEnter a number: "))
print('\n') 

for x in range(n):
 b=input('Enter tuple two elements: ')
 _secondTuple_=_secondTuple_ + (a,)

print('First tuple: ',_firstTuple_)
print('Second tuple: ',_secondTuple_) 
_thirdTuple_=(_firstTuple_+_secondTuple_)
print('Third tuple: ',_thirdTuple_)
6. Write a program as per following specification: return the length of the shortest string in the tuple of strings str_tuple. Precondition: the tuple will contain at least one element.
str_tuple =("this is a string", "this is another string", "this is a string too")
shortestStrlen=min(str_tuple)
print(len(shortestStrlen))
7. Create the following tuples using a for loop:

(a) A tuple containing the squares of the integers 1 through 50.

myList=[i*i for i in range(1,51)]
myTuple=tuple(myList)
print(myTuple)
(b) The Tuple ['a','bb','ccc','dddd',...] that ends with 26 copies of the letter z.
import string
listABCD=string.ascii_lowercase
count=1
newList=[]
for i in listABCD:
 newList.append(i*count)
 count+=1
print tuple(newList)
8. 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.
NewTuple=((2,5),(4,2),(9,8),(12,10))
count=0
for i,j in NewTuple:
   if (i%2==0) and (j%2==0):
     print(i,j)
     count=count+1
     
print('So the number of even pairs in NewTuple are : ', count)     
09. Write a program that inputs two tuples seq_a and seq_b and prints True if every element in seq_a is also an element of seq_b, else prints False.
def result(index):
  if index==-1:
  print("False")
  else:
  print("True")

seq_a=(123, 'xyz')
seq_b=(456, 'abc')

print("seq_a = ", seq_a)
print("seq_b = ",seq_b)
result(cmp(seq_a, seq_b))

seq_a=(123, 'abc')
seq_b=(123, 'abc')

print("seq_a = ", seq_a)
print("seq_b = ",seq_b)
result(cmp(seq_a,seq_b))
       
10. Write a program that calculates and displays the mean of a tuple with numeric elements
sumOfNum=0
mean=0
numTuple=(34,22,55,53)
print("The tuple is: ")
print(numTuple)
sumOfNum=sum(numTuple)
mean=sumOfNum/len(numTuple)

print("Its mean is: ")
print(mean)
11. Given a nested tuple tup1 = ((1, 2), (3, 4.15, 5.15), (7, 8, 12, 15)). Write a program that displays the means of individual elements of tuple tup1 and then displays the mean of these compund means. That is for above tuple, it should display as:
Mean element 1 : 1.5 ; Mean element 2 : 4.1
Mean element 1: 10.5; Mean of means 5.366666

tup1=((1,2),(3,4.15,5.15),(7,8,12,15))
sumOfMeans=0
mean=0
(x,y,z)=tup1
def mean(x):
 mean=float(sum(x))/len(x)
 return mean 
print("Mean of ", x ,"is : ",mean(x) )
print("Mean of ", y ,"is : ", mean(y))
print("Mean of ", z ,"is : ",mean(z))
sumOfMeans=(mean(x)+mean(y)+mean(z))/3
print("Sum of means: ", sumOfMeans)      
12. Write a program to compute the standard deviation for the numeric samples collected in a tuple.
import math
sumOfNum=0
mean=0
numTuple=(34,22,55,53)
print("The tuple is: ")
print(numTuple)
sumOfNum=sum(numTuple)
mean=sumOfNum/len(numTuple)

print("Its mean is: ")
print(mean)

diff1=0
#newlist=list(numTuple)
for i in numTuple:
 diff1=diff1+((i-mean)**2)

diff1=diff1/(len(numTuple)-1)
diff1=math.sqrt(diff1)
print('Standard deviation:', "{0:.2f}".format(diff1))      

Comments

Post a Comment

Popular posts from this blog

Chapter 05 - File Handling

Dictionary (Python)