Python Tuple assignment with solutions

Python Tuple assignment is based on Python Tuples dataType. This Python Tuple assignment will help you to check your Python Tuples. Python TuplesPython Tuple are one of the code data types that is being used in all modern libraries.

Q1. A tuple is a mutable data type ( True/ False)

Show Answer

True

Q2. Define a blank tuple t1

Show Answer

t1=tuple( )

Q3. Find out the correct way to declare a tuple with one element

  1. T1 = 10
  2. T1 = (10)
  3. T1= (10,)
  4. Both b and c
Show Answer

  Option -C

Q4.  A tuple t1 contains the following element
t1  = ( 10,20,’rakesh’, 12.34,’a’, 40,50)
find out the output of the following  commands

  1. Print(t1[2])
  2. Print(t1[2][2])
  3. Print(t1[-4])
  4. Print(t1[:5])
  5. Print(t1[1:4]
  6. Print(t1[::2]
  7. Print(t1[::-1]

Q5. Find out the output of the following code
t1  = ( 10,20,’rakesh’, 12.34,’a’, 40,50)
t2 =  (20,30,40)

  1. T2 = t1+t2
  2. Print(t1*2)

Q6. Find out the output of the following code segments
t1  = ( 10,20,’rakesh’, 12.34,’a’, 40,50)
t2 =  (20,30,40)

  1. Print(t1>t2)
  2. Print(len(t1)>(t2))
  3. Print (t1==t2)

Q7. Find out the output of the following code segment
t1  = ( 10,20,’rakesh’, 12.34,’a’, 40,50,’a’)

if ‘a’ in t1:
    print(‘ found’, t1.count(‘a’), ‘times’)
else:
    print(‘ Element does not exists’)

Q8. Suppose a tuple t1 has the following elements. Find out the sum of all the numbers in this tuple
t1 = ( 10,20,’rakesh’, 12.34,”a”, 40,50)

Solution

t1 = ( 10,20,'rakesh', 12.34,'a', 40,50)
s=0
for x in t1:
    if type(x)==int or type(x)==float:
       s = s+x
print(' sum of numbers :',s)

Output : 132.34

Q9. Write a program in python to find out the largest number from a tuple of numbers without using built in method max( )

Solution

t1=(45,657,67,3,434,3,56,6,87,8,45,4345,233,5)
lar = t1[0]
for x in range(1,len(t1)):
    if t1[x]>lar:
       lar = t1[x]

print('Largest No :',lar) 

Q10. Write a program in python to find out the sum of all the odd numbers available in a tuple

Solution

t1=(45,657,67,3,434,3,56,6,87,8,45,4345,233,5)
odd=0
for x in t1:
  if x%2==0:
    odd+=x
print(' Sum of odd Numbers :',odd)

Q11.  Write a program in Python to find out an element from a tuple of number. Print number is available if number exists in our tuple otherwise display ‘ Number does not exists’ using linear search method. ( Do not use membership operator)

Solution

t1=(45,657,67,3,434,3,56,6,87,8,45,4345,233,5)
data = 34
found =0

for x in t1:
    if x==data:
       found =1
if found==1:
  print('Data Present in this tuple')
else:
  print(' Data does not exist')

OUTPUT :

Data does not exist

Q12. Write a program in Python to find out the frequency of an element in any given tuple. ( Do not use built in method count() )

Solution

t1=(45,657,67,3,434,3,56,6,67,8,45,4345,67,5)
data = 67
found =0

for x in t1:
    if x==data:
       found +=1
if found!=0:
  print('Data Present in this tuple ',found,' times')
else:
  print(' Data does not exist')

OUTPUT:

Data Present in this tuple  3  times

This Python Tuple assignment will check all your tuples skills and help you to understand tuples in a better way.

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.