Tuples

This page contains the CBSE Computer Science with Python class 11 Unit-4 chapter 4 Tuples. You can find the solutions for chapter 4 of CBSE class 11 Computer Science with Python Exercise. So is the case if you are looking for CBSE class 11 Computer Science with Python related topic Tuples questions and answers for the Exercise.
Question 1
1. Write the output from the following codes;
(i)
t=(10,20,30,40,50)
print len(t)
(ii)
t=('a','b','c','A','B')
max(t)
min(t)
(iii)
T1=(10,20,30,40,50)
T2 =(10,20,30,40,50)
T3 =(100,200,300)
cmp(T1,T2)
cmp(T2,T3)
cmp(T3,T1)
(iv)
t=tuple()
Len(t)
(v)
T1=(10,20,30,40,50)
T2=(100,200,300)
T3=T1+T2
print T3
Answer 1
A tuple is an ordered sequence of values enclosed in parentheses, and it is immutable, which means its elements cannot be changed directly after creation. Functions like len(), max(), min(), and operations like concatenation can still be used on tuples.
(i)
t = (10, 20, 30, 40, 50)
print len(t)
Output:
5
Explanation:len(t) returns the total number of elements in the tuple. Since the tuple has five values, the result is 5.
(ii)
t = ('a', 'b', 'c', 'A', 'B')
print max(t)
print min(t)
Output:
c
A
Explanation: For string characters, max() and min() are determined according to character ordering. In Python, uppercase and lowercase letters are compared by their character codes, so among these values:
the greatest value is 'c'
the smallest value is 'A'
This is because uppercase letters come before lowercase letters in character ordering.
(iii)
T1 = (10, 20, 30, 40, 50)
T2 = (10, 20, 30, 40, 50)
T3 = (100, 200, 300)

print cmp(T1, T2)
print cmp(T2, T3)
print cmp(T3, T1)
Output:
0
-1
1
Explanation:cmp() compares two tuples element by element.
cmp(T1, T2) gives 0 because both tuples are exactly the same.
cmp(T2, T3) gives -1 because the first differing element is 10, which is smaller than 100.
cmp(T3, T1) gives 1 because 100 is greater than 10.
So, the result shows equal, smaller, and greater comparisons respectively.
(iv)
t = tuple()
Len(t)
Answer: This gives an error.
Explanation: The correct function name is len(), not Len(). Python is case-sensitive, so Len is treated as a different name and is not recognized as the built-in function. Therefore, this causes a NameError. The correct form is:
len(t)
and that would return 0 because the tuple is empty.
(v)
T1 = (10, 20, 30, 40, 50)
T2 = (100, 200, 300)
T3 = T1 + T2
print T3
Output:
(10, 20, 30, 40, 50, 100, 200, 300)
Explanation: The + operator joins two tuples and creates a new tuple containing all elements of both tuples in sequence. Since tuples are immutable, the original tuples do not change; instead, the result is stored in T3.
Question 2
2. Write a program to input two set values and store it in tuples and also do the comparison.
Answer 2
This question asks to take two groups of values, store them in two tuples, and then compare them. In tuple comparison, Python compares the tuples element by element. If both tuples are equal, the result is equality; otherwise, one may be greater or smaller depending on the first differing element. The chapter also demonstrates tuple comparison using cmp().
Program
# Program to input two sets of values in tuples and compare them

t1 = tuple()
t2 = tuple()

n1 = input("Total number of values in first tuple: ")
for i in range(n1):
    a = input("Enter element: ")
    t1 = t1 + (a,)

n2 = input("Total number of values in second tuple: ")
for i in range(n2):
    a = input("Enter element: ")
    t2 = t2 + (a,)

print "First Tuple =", t1
print "Second Tuple =", t2

if t1 == t2:
    print "Both tuples are equal"
elif t1 > t2:
    print "First tuple is greater"
else:
    print "Second tuple is greater"
Sample Output
Total number of values in first tuple: 3
Enter element: 10
Enter element: 20
Enter element: 30
Total number of values in second tuple: 3
Enter element: 10
Enter element: 25
Enter element: 30
First Tuple = (10, 20, 30)
Second Tuple = (10, 25, 30)
Second tuple is greater
Explanation
t1 and t2 are initially empty tuples.
New elements are added by using t = t + (a,).
The tuples are then compared.
If they are not equal, the first differing element decides which tuple is greater.
Thus, the program stores values in tuple form and performs comparison correctly.
Question 3
3. Write a program to input ‘n’ employees’ salary and find minimum & maximum salary among ‘n’ employees.
Answer 3
This question asks to store the salaries in a tuple and then use the built-in functions max() and min() to find the highest and lowest salary. The chapter gives a very similar example for numbers in a tuple.
Program
# Program to input employees' salaries and find minimum and maximum salary

t = tuple()
n = input("Total number of employees: ")

for i in range(n):
    salary = input("Enter salary: ")
    t = t + (salary,)

print "Maximum salary =", max(t)
print "Minimum salary =", min(t)
Sample Output
Total number of employees: 4
Enter salary: 25000
Enter salary: 18000
Enter salary: 32000
Enter salary: 22000
Maximum salary = 32000
Minimum salary = 18000
Explanation
Each salary is added to the tuple one by one.
max(t) returns the largest salary value.
min(t) returns the smallest salary value.
So, this program helps in finding the highest and lowest salary from the entered data.
Question 4
4. Find the errors from the following code:
t=tuple{}
n=input(Total number of values in tuple)
for i in range(n)
a=input("enter elements")
t=t+(a)
print "maximum value=",max(t)
print "minimum value=",min(t)
Answer 4
Given code:
t = tuple{}
n = input(Total number of values in tuple)
for i in range(n)
    a = input("enter elements")
    t = t + (a)
print "maximum value=", max(t)
print "minimum value=", min(t)
This program contains several syntax and logical errors. The correct answer is to identify those mistakes and rewrite the code properly.
Errors
1.
t = tuple{} is incorrect. The correct way to create an empty tuple is:
   t = tuple()
12.
The prompt in input() must be inside quotation marks:
   n = input("Total number of values in tuple")
3.
The for statement is missing a colon ::
   for i in range(n):
4.
t = t + (a) is incorrect for adding one element to a tuple. A single-element tuple must have a comma:
   t = t + (a,)
5.
Proper indentation is required inside the loop.
Corrected Program
# Corrected tuple program

t = tuple()
n = input("Total number of values in tuple")

for i in range(n):
    a = input("Enter element: ")
    t = t + (a,)

print "maximum value=", max(t)
print "minimum value=", min(t)
Explanation
tuple() creates an empty tuple.
A single element must be written as (a,) while adding it to a tuple.
max() and min() then work correctly on the completed tuple.
Thus, after correcting the syntax and tuple creation errors, the program runs properly.
Question 5
5. Write a program to input ‘n’ customers’ name and store it in tuple and display all customers’ names on the output screen.
Answer 5
This question asks to take customer names as input, store them in a tuple, and then display them. Since tuples are immutable, a new one-element tuple is added each time to build the final tuple.
Program
# Program to input customer names and store them in a tuple

t = tuple()
n = input("Total number of customers: ")

for i in range(n):
    name = raw_input("Enter customer name: ")
    t = t + (name,)

print "Customer names are:"
for i in range(n):
    print t[i]
Sample Output
Total number of customers: 3
Enter customer name: Amit
Enter customer name: Neha
Enter customer name: Rahul
Customer names are:
Amit
Neha
Rahul
Explanation
The program starts with an empty tuple.
Each customer name is added by using t = t + (name,).
Finally, the tuple is traversed by index and all names are displayed.
Thus, the program stores text values in a tuple and prints them one by one.
Question 6
6. Write a program to input ‘n’ numbers and separate the tuple in the following manner.
Example
T=(10,20,30,40,50,60)
T1 =(10,30,50)
T2=(20,40,60)
Answer 6
The question gives an example like:
T = (10, 20, 30, 40, 50, 60)
T1 = (10, 30, 50)
T2 = (20, 40, 60)
This means the original tuple is to be split into two tuples:
one containing elements at odd positions (1st, 3rd, 5th, …)
the other containing elements at even positions (2nd, 4th, 6th, …)
Program
# Program to separate a tuple into two tuples

t = tuple()
t1 = tuple()
t2 = tuple()

n = input("Total number of values in tuple: ")

for i in range(n):
    a = input("Enter number: ")
    t = t + (a,)

for i in range(n):
    if i % 2 == 0:
        t1 = t1 + (t[i],)
    else:
        t2 = t2 + (t[i],)

print "Original Tuple =", t
print "T1 =", t1
print "T2 =", t2
Sample Output
Total number of values in tuple: 6
Enter number: 10
Enter number: 20
Enter number: 30
Enter number: 40
Enter number: 50
Enter number: 60
Original Tuple = (10, 20, 30, 40, 50, 60)
T1 = (10, 30, 50)
T2 = (20, 40, 60)
Explanation
The original tuple t stores all entered numbers.
If the index is even (0, 2, 4, ...), the value is added to t1.
If the index is odd (1, 3, 5, ...), the value is added to t2.
Since indexing starts from 0, the element at index 0 is the first entered value. Therefore, t1 gets the 1st, 3rd, 5th values, and t2 gets the 2nd, 4th, 6th values.
Thus, the tuple is separated exactly in the way shown in the example.