TUPLE (1)
TUPLE (1)
WORKSHEET ON TUPLE
MCQ:
1. Which of the following will create a single element tuple? [SUMITA ARORA]
(a) (1,) (b) (1) (c) ([1]) (d)tuple([1])
2. What will be the output of the following python code? [SUMITA ARORA]
tp1=(2,4,3)
tp3=tp1*2
print(tp3)
4. Which of the below given functions cannot be used with nested tuples? [SUMITA ARORA]
(a) index() (b)count() (c)max() (d)sum()
Based on the following assertion (A) and reasoning(R), pick up an appropriate statement
from the options below:
(a) Both A and R are true and R is the correct explanation of A.
(b) Both A and R are true and R is not the correct explanation of A.
(c) A is true but R is false
(d) Both A and R are false.
8. Assertion (A): Indexing of tuple elements means the positioning of the elements in a
tuple.
Reason(R): The indexing of tuple elements can be defined in two ways. In the forward
indexing, the index of the elements starts with 1, whereas the backward indexing starts
with -1.
9. Assertion (A): The sorted() function is used to arrange the set of elements of atuple
either in the ascending or descending order.
Reason(R): The tuple is an mutable set of data. Therefore the tuple gets converted
implicitly into a list before getting used as argument to the sorted() function.Finaly the
sorted elements appear as a list.
10. Assertion (A): The index() function returns the index of the first occurrence of an
element in the tuple.
Reason(R): This function will start searching from the 0th index of the tuple and return
the index of the last occurrence of an element.
11. Assertion (A): You can also compare two tuples using relational operators.
Reason(R): The relation operators such as ==,<,<=,>,>= etc are used to compare the
tuple elements with each other starting with the 0th index. The result of the comparison
will either be true or false.
14. Your brother is trying to understand a code snippet while preparing for his examination.
The code snippet is as given below:
T1=eval(input())
tln=len(T1)
for x in T1:
if(x<10)
c1=c1+1
elif(x<100)
c2=c2+1
else:
c3=c3+1
He has some queries in his mind about some of the statements used in the above code
snippet. Answer his queries to help him to understand the logic. His queries as a listed
below:
(a) Which statement of the snippet finds the number of elements available in the tuple?
(b) Which statement of the snippet has been used to enter the values in the tuple?
(c) Which statement has been used to iterate for getting the values from different indices
of the tuple?
(d) Which statement of the code snippet counts the number of two digit numbers?
17. A tuple T1(1,2,3,4,5) contains the five integers. What will be the result when the
following statements are executed? [VK PANDAY]
(i) T1.index(3)+len(T1)
(ii) T1[1]+T1[4]
(iii) len(T1*2)
18. Carefully read the given code fragments and figure out the errors that the code may
produce. [SUMITA ARORA]
(a) t3 = (6, 7)
t4 = t3*3
t5 = t3 * (3)
t6 = t3 * (3,)
print(t4)
print(t5)
print(t6)
20. What will be the output of the following code snippet? [SUMITA ARORA]
Tup1 = ((1, 2),) * 7
Print(len(Tup1 [3:8]))
PROGRAM:
31. Write a program that receives the index and returns the corresponding value.
[SUMITA ARORA]
32. 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 5 th term; for 8, it returns 7.
[SUMITA ARORA]
33. 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 : [SUMITA ARORA]
34. In the program created in previous question, add a function that computes total marks and
average marks obtained by each student. [SUMITA ARORA]
35. 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. [SUMITA ARORA]
36. 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. [SUMITA ARORA]
37. Write a Python code to accept different strings in a tuple. Display the strings that start
and end with the same letter. [VK PANDAY]
38. Write a Python code to accept a set of numbers in a tuple. Display all the unique
numbers. [VK PANDAY]
39. Write a program in Python to accept the names of some countries and their capitals in
two different tuples respectively. Display the names of countries along with their capitals.
The program also checks whether the information is displayed correctly or not by
entering country name (say ‘India’) to display capital name(say ‘New Delhi’). [VK PANDAY]
40. Write a Python program to accept some string in a tuple. Find and display the length of
the longest string. [VK PANDAY]
HOME WORK:
42. Write a program to read email IDs of n number of students and store them in a tuple.
Create two new tuples, one to store only the usernames from the email IDs and second to
store domain names from the email IDs. Print all three tuples at the end of the program.
[Hint: You may use the function split()]
43. Write a program to input names of n students and store them in a tuple. Also, input a
name from the user and find if this student is present in the tuple or not.
We can accomplish these by:
(a) writing a user defined function
(b) using the built-in function
44. A bank is a financial institution which is involved in borrowing and lending of money.
With advancement in technology, online banking, also known as internet banking allows
customers of a bank to conduct a range of financial transactions through the bank’s
website anytime, anywhere. As part of initial investigation you are suggested to
• collect a bank’s application form. After careful analysis of the form, identify the
information required for opening a savings account. Also enquire about the rate of
interest offered for a saving account.
• The basic two operations performed on an account are Deposit and Withdrawal. Write
a menu driven program that accepts either of the two choices of Deposit and Withdrawal,
then accepts an amount, performs the transaction and accordingly displays the balance.
Remember, every bank has a requirement of minimum balance which needs to be taken
care of during withdrawal operations. Enquire about the minimum balance required in
your bank.
• Collect the interest rates for opening a fixed deposit in various slabs in a savings bank
account. Remember, rates may be different for senior citizens.
Finally, write a menu driven program having the following options (use functions and
appropriate data types):
• Open a savings bank account
• Deposit money
• Withdraw money
• Take details, such as amount and period for a Fixed Deposit and display its maturity
amount for a particular customer.
47. Prove with the help of an example that the variable is rebuilt in case of immutable data
types.
48. TypeError occurs while statement 2 is running. Give reason. How can it be corrected?
>>> tuple1 = (5) #statement 1
>>> len(tuple1) #statement 2
49. Write a program to input ‘n’ numbers in a tuple and separate the tuple in the following
manner.
Example
T=(10,20,30,40,50,60)
Tl=(10,30,50)
T2=(20,40,60)
50. Write a program to input two tuple and find the intersection of both.
Example:
Input:
Tl=(10,30,50,40,60)
T2=(20,40,60,70)
Output:
Intersection:(40,60)