0% found this document useful (0 votes)
52 views13 pages

Tuples Type B

Uploaded by

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

Tuples Type B

Uploaded by

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

Tuples in Python

Type B Questions:
Question 1(a): Find the output generated by following code
fragments :
plane = ("Passengers", "Luggage")
plane[1] = "Snakes"
Output
TypeError: 'tuple' object does not support item assignment
Explanation
Since tuples are immutable, tuple object does not support item
assignment.
Question 1(b): Find the output generated by following code
fragments :
(a, b, c) = (1, 2, 3)
print(“a =”,a)
print(“b =”,b)
print(“c =”,c)
Output
a=1
b=2
c=3
Explanation
When we put tuples on both sides of an assignment operator, a tuple
unpacking operation takes place. The values on the right are
assigned to the variables on the left according to their relative
position in each tuple. As you can see in the above example, a will be
1, b will be 2, and c will be 3.
Question 1(c): Find the output generated by following code
fragments :
(a, b, c, d) = (1, 2, 3)
Answer
Output
ValueError: not enough values to unpack (expected 4, got 3)
Explanation
Tuple unpacking requires that the list of variables on the left has the
same number of elements as the length of the tuple. In this case, the
list of variables has one more element than the length of the tuple so
this statement results in an error.
Question 1(g): Find the output generated by following code
fragments:
t2 = ('a')
print(type(t2))
Answer
Output
<class 'str'>
Explanation
The type() function is used to get the type of an object. Here, 'a' is
enclosed in parenthesis but comma is not added after it, hence it is
not a tuple and belong to string class.
Question 1(h): Find the output generated by following code
fragments :
t3 = ('a',)
print(type(t3))
Answer: Output
<class 'tuple'>
Explanation
Since 'a' is enclosed in parenthesis and a comma is added after it, so
t3 becomes a single element tuple instead of a string.
Question 1(i): Find the output generated by following code
fragments :
T4 = (17)
print(type(T4))
Answer: Output
<class 'int'>
Explanation
Since no comma is added after the element, so even though it is
enclosed in parenthesis still it will be treated as an integer, hence T4
stores an integer not a tuple.
Question 1(j): Find the output generated by following code
fragments :
T5 = (17,)
print(type(T5))
Answer: Output
<class 'tuple'>
Explanation
Since 17 is enclosed in parenthesis and a comma is added after it, so
T5 becomes a single element tuple instead of an integer.
Question 1(k): Find the output generated by following code
fragments :
tuple = ( 'a' , 'b', 'c' , 'd' , 'e')
tuple = ( 'A', ) + tuple[1: ]
print(tuple)
Answer: Output
('A', 'b', 'c', 'd', 'e')
Explanation
tuple[1:] creates a tuple slice of elements from index 1 (indexes
always start from zero) to the last element i.e. ('b', 'c', 'd', 'e').
+ operator concatenates tuple ( 'A', ) and tuple slice tuple[1: ] to
form a new tuple.
Question 1(l): Find the output generated by following code
fragments :
t2 = (4, 5, 6)
t3 = (6, 7)
t4 = t3 + t2
t5 = t2 + t3
print(t4)
print(t5)
Answer: Output
(6, 7, 4, 5, 6)
(4, 5, 6, 6, 7)
Explanation
Concatenate operator concatenates the tuples in the same order in
which they occur to form new tuple. t2 and t3 are concatenated
using + operator to form tuples t4 and t5.
Question 1(m): Find the output generated by following code
fragments:
t3 = (6, 7)
t4 = t3 * 3
t5 = t3 * (3)
print(t4)
print(t5)
Answer: Output
(6, 7, 6, 7, 6, 7)
(6, 7, 6, 7, 6, 7)
Explanation
The repetition operator * replicates the tuple specified number of
times. The statements t3 * 3 and t3 * (3) are equivalent as (3) is an
integer not a tuple because of lack of comma inside parenthesis.
Both the statements repeat t3 three times to form tuples t4 and t5.
Question 1(n): Find the output generated by following code
fragments:
t1 = (3,4)
t2 = ('3' , '4')
print(t1 + t2 )
Answer: Output
(3, 4, '3', '4')
Explanation
Concatenate operator + combines the two tuples to form new tuple.
Question 1(o): Skipped
Question 2: What does each of the following expressions evaluate
to? Suppose that T is the tuple containing :
("These", ["are" , "a", "few", "words"] , "that", "we", "will" , "use")
1. T[1][0: :2]
2. "a" in T[1][0]
3. T[:1] + [1]
4. T[2::2]
5. T[2][2] in T[1]
Answer
The given expressions evaluate to the following:
1. ['are', 'few']
2. True
3. TypeError: can only concatenate tuple (not "list") to tuple
4. ('that', 'will')
5. True
Question 3: Carefully read the given code fragments and figure out
the errors that the code may produce.
(a)
t = ('a', 'b', 'c', 'd', 'e')
print(t[5])
Answer: Output
IndexError: tuple index out of range
Explanation
Tuple t has 5 elements starting from index 0 to 4. t[5] will throw an
error since index 5 doesn't exist.
(b)
t = ('a', 'b', 'c', 'd', 'e')
t[0] = 'A'
Answer
Output
TypeError: 'tuple' object does not support item assignment
Explanation
Tuple is a collection of ordered and unchangeable items as they are
immutable. So once a tuple is created we can neither change nor add
new values to it.
(c)
t1 = (3)
t2 = (4, 5, 6)
t3 = t1 + t2
print (t3)
Answer
Output
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
Explanation
t1 holds an integer value not a tuple since comma is not added after
the element where as t2 is a tuple. So here, we are trying to use +
operator with an int and tuple operand which results in this error.
(d)
t1 = (3,)
t2 = (4, 5, 6)
t3 = t1 + t2
print (t3)
Answer
Output
(3, 4, 5, 6)
Explanation
t1 is a single element tuple since comma is added after the element
3, so it can be easily concatenated with other tuple. Hence, the code
executes successfully without giving any errors.
(e)
t2 = (4, 5, 6)
t3 = (6, 7)
print(t3 - t2)
Answer
Output
TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
Explanation
Arithmetic operations are not defined in tuples. Hence we can't
remove items in a tuple.
(f)
t3 = (6, 7)
t4 = t3 * 3
t5= t3 * (3)
t6 = t3 * (3,)
print(t4)
print(t5)
print(t6)
Answer
Output
TypeError: can't multiply sequence by non-int of type 'tuple'
Explanation
The repetition operator * replicates the tuple specified number of
times. The statements t3 * 3 and t3 * (3) are equivalent as (3) is an
integer not a tuple because of lack of comma inside parenthesis.
Both the statements repeat t3 three times to form tuples t4 and t5.
In the statement, t6 = t3 * (3,), (3,) is a single element tuple and we
can not multiply two tuples. Hence it will throw an error.
(g)
odd= 1,3,5
print(odd + [2, 4, 6])[4]
Answer
Output
TypeError: can only concatenate tuple (not "list") to tuple
Explanation
Here [2,4,6] is a list and odd is a tuple so because of different data
types, they can not be concatenated with each other.
(h)
t = ( 'a', 'b', 'c', 'd', 'e')
1, 2, 3, 4, 5, = t
Answer
Output
SyntaxError: cannot assign to literal
Explanation
When unpacking a tuple, the LHS (left hand side) should contain a list
of variables. In the statement, 1, 2, 3, 4, 5, = t, LHS is a list of literals
not variables. Hence, we get this error.
(i)
t = ( 'a', 'b', 'c', 'd', 'e')
1n, 2n, 3n, 4n, 5n = t
Answer
Output
SyntaxError: invalid decimal literal
Explanation
This error occurs when we declare a variable with a name that starts
with a digit. Here, t is a tuple containing 5 values and then we are
performing unpacking operation of tuples by assigning tuple values
to 1n, 2n, 3n, 4n, 5n which is not possible since variable names
cannot start with numbers.
Question 4
What would be the output of following code if
ntpl = ("Hello", "Nita", "How's", "life?")
(a, b, c, d) = ntpl
print ("a is:", a)
print ("b is:", b)
print ("c is:", c)
print ("d is:", d)
ntpl = (a, b, c, d)
print(ntpl[0][0]+ntpl[1][1], ntpl[1])
Answer
Output
a is: Hello
b is: Nita
c is: How's
d is: life?
Hi Nita
Explanation
ntpl is a tuple containing 4 elements. The statement (a, b, c, d) =
ntpl unpacks the tuple ntpl into the variables a, b, c, d. After that, the
values of the variables are printed.
The statement ntpl = (a, b, c, d) forms a tuple with values of variables
a, b, c, d and assigns it to ntpl. As these variables were not modified,
so effectively ntpl still contains the same values as in the first
statement.
ntpl[0] ⇒ "Hello"
∴ ntpl[0][0] ⇒ "H"
ntpl[1] ⇒ "Nita"
∴ ntpl[1][1] ⇒"i"
ntpl[0][0] and ntpl[1][1] concatenates to form "Hi".
Thus ntpl[0][0]+ntpl[1][1], ntpl[1] will return "Hi Nita ".
Question 5
Predict the output.
tuple_a = 'a', 'b'
tuple_b = ('a', 'b')
print (tuple_a == tuple_b)
Answer
Output
True
Explanation
Tuples can be declared with or without parentheses (parentheses are
optional). Here, tuple_a is declared without parentheses where as
tuple_b is declared with parentheses but both are identical. As both
the tuples contain same values so the equality operator ( == ) returns
true.
Question 6
Find the error. Following code intends to create a tuple with three
identical strings. But even after successfully executing following
code (No error reported by Python), The len( ) returns a value
different from 3. Why ?
tup1 = ('Mega') * 3
print(len(tup1))
Answer
Output
12
Explanation
This is because tup1 is not a tuple but a string. To make tup1 a tuple
it should be initialized as following:
tup1 = ('Mega',) * 3
i.e., a comma should be added after the element.
We are getting 12 as output because the string "Mega" has four
characters which when replicated by three times becomes of length
12.
Question 7:
Predict the output.
tuple1 = ('Python') * 3
print(type(tuple1))
Answer
Output
<class 'str'>
Explanation
This is because tuple1 is not a tuple but a string. To make tuple1 a
tuple it should be initialized as following:
tuple1 = ('Python',) * 3
i.e. a comma should be added after the element.
Question 8: Skipped
Question 9:
What will the following code produce?
Tup1 = (1,) * 3
Tup1[0] = 2
print(Tup1)
Answer
Output
TypeError: 'tuple' object does not support item assignment
Explanation
(1,) is a single element tuple. * operator repeats (1,) three times to
form (1, 1, 1) that is stored in Tup1.
Tup1[0] = 2 will throw an error, since tuples are immutable. They
cannot be modified in place.
Question 10:
What will be the output of the following code snippet?
Tup1 = ((1, 2),) * 7
print(len(Tup1[3:8]))
Answer
Output
4
Explanation
* operator repeats ((1, 2),) seven times and the resulting tuple is
stored in Tup1. Therefore, Tup1 will contain ((1, 2), (1, 2), (1, 2), (1,
2), (1, 2), (1, 2), (1, 2)).
Tup1[3:8] will create a tuple slice of elements from index 3 to index 7
(excluding element at index 8) but Tup1 has total 7 elements, so it
will return tuple slice of elements from index 3 to last element i.e ((1,
2), (1, 2), (1, 2), (1, 2)).
len(Tup1[3:8]) len function is used to return the total number of
elements of tuple i.e., 4.
Extras
Q1. Following are the statements for cresting tuples, find the errors
and rewrite the same after correcting them.
(a) tup1 = 1, 6, a, 8
(b) tup2 = (10)
(c) tup1 = (0, 1, 2, 3) [“my”, ”book”])
(d) tup1 = (0, 1, 2, 3 (4, 5, 6))
(e) tup1 = (“a”, “b”, “c” [1, 2,3])
Answers:
(a) tup1 = 1, 6, “a”, 8
(b) tup2 = (10,)
(c) tup1 = (0, 1, 2, 3), [“my”, ”book”])
(d) tup1 = (0, 1, 2, 3, (4, 5, 6))
(e) tup1 = (“a”, “b”, “c”, [1, 2,3])
Q2. Consider the following code and find the error.
(a)
tup1 = (1,"school",6, 7)
print(max(tup1))
(b)
t = (1, "s", "h", 5, "p")
t[1] = "o"
(c)
T1 = 7
T2 = (1, 2, 3)
T3 = T1 + T2
(d)
T1 = (1,2,3)
T2 = (4,5,6)
T3 = (8,9)
a,b,c = T1, T2
Answers:
(a) max() will only work if the elements in a tuple are of the same
data type.
(b) Tuples are immutable, we cannot change the value of any
element of the tuple.
(c) ‘+’ operator joins two tuples, here (7) is ‘int’, so will give error.
(d) For the tuple unpacking, left hand side and right hand side
element should be the same. T3 is not there on the right hand side
Q2. Consider the below tuple ‘t1’ and answer the following
questions:
t1 = (10, 20, "book", 30, 9.5, "item", [12, 13], (3, 4), 30, 5, 30)
(a) len(t1) (b) t1[ : 6]
(c) t1[ -8 : -4 ] (d) t1 [ 5 : ]
(e) t1.index(20) (f) t1.index(30)
(g) t1.index(30, 7, 10) (h) t1.count(30)
(i) t1 [ : 1] * 5
Answers:
(a) 11 (b) (10, 20, ‘book’, 30, 9.5, ‘item’)
(c) (30, 9.5, ‘item’, [12, 13]) (d) (‘item’, [12, 13], (3, 4), 30, 5, 30)
(e) 1 (f) 3
(g) 8 (h) 3
(i) (10, 10, 10, 10, 10)

You might also like