Chapter – 8: TUPLES
Very Short answer Type
Questions Q.1 What do you understand by immutability?
Ans: Immutability means not changeable. The immutable types are
those that can never change their value in place. In python following types are
immutable –
I.
Integers
II.
Floating point numbers
III.
Booleans
IV.
Strings
V.
Tuples
Q.2 What is the length of the
tuple shown below?
T = ((((„a‟, 1), ‟b‟, ‟c‟), ‟d‟, 2), ‟e‟,
3) Ans: 3, because tuple contains
only 3 elements.
Q.3 If a is (1, 2, 3), what is
the difference (if any) between a*3 and [a, a, a]?
Ans: a*3 is different from [a,a,a] because, a*3 will produce a
tuple (1,2,3,1,2,3,1,2,3) and [a, a, a]
will produce a list of tuples
[(1,2,3),(1,2,3),(1,2,3)].
Q.4 If a is (1, 2, 3), is a *3
equivalent to a + a + a?
Ans: Yes
Q.5
If a is (1, 2, 3), what is the meaning of a [1:1] = 9?
Ans:
This will produce an error: TypeError: 'tuple' object does not support item
assignment Q.6 If a is (1, 2, 3), what
is the meaning of a [1:2] = 4 and a [1:1] = 4?
Ans:
This will produce an error: TypeError: 'tuple' object does not support item
assignment Q.7 Does a slice operator
always produce a new Tuple?
Ans: Yes
Q.8
How is an empty Tuple created?
Ans: To create an empty tuple we have to write –
>>>T=() or
>>>T=tuple()
Q.9 How is a tuple
containing just one element created?
Ans: Following methods may
be used to create single valued tuple –
>>>T=3,
or
>>>T=(4,)
Q.10 What is the difference between (30)
and (30,)?
Ans: (30) is an integer while (30,) is a tuple.
Short Answer Type Questions
Q.1
How are Tuples different from Lists
when both are sequences?
Ans: Tuples are similar to lists in many ways like indexing,
slicing, and accessing individual values
but
they are different in the sense that – Tuples are immutable while lists are
mutable.
List can grow or shrink
while tuples cannot. Q.2 Can tuples be nested?
Ans: Yes, tuples can be
nested. e.g (1,2,3,(4,5,6))
Q.3 Discuss the utility and
significance of Tuples.
Ans: Tuples are great for holding static data that you don't plan
on modifying and changing a lot. Tuples are four to seven times faster than
lists. This means for every list item manipulated, four to seven tuples could
be manipulated within that time frame. This has huge advantages for scientific
work, as this allows for speedy data reading.
Q.4
How can you say that a tuple is an
ordered list of objects?
Ans: A tuple is an ordered
list of objects. This is evidenced by the fact that the objects can be accessed
through the use of an ordinal index amd for a given index, same element is
returned everytime.
Q.5 How can you add an extra
element to a tuple?
Ans: We can
add an extra element to the tuple as
follows – >>>T = T +(9,)
e.g.
Q.6 Find
out the output generated by following code fragments: (a)
plane = (“Passengers”, ”Luggage”) plane [1] = “Snakes”
(b) (a, b, c) = (1,2,3) (c)
(a, b, c, d) = (1,2,3)
(d) a, b, c, d = (1,2,3) (e) a,
b, c, d, e = (p, q, r, s, t) = t1
Ans: (a) TypeError: 'tuple' object does
not support item assignment (b) This
will assign 1 to a, 2 to b and 3 to c.
(c) ValueError:
not enough values to unpack (expected 4, got 3)
(d) ValueError:
not enough values to unpack (expected 4, got 3)
(e) If
tuple t1 has 5 values then this will assign first value of t1 in to a and p ,
next value to b and q and so on.
Q.7
Predict the
output –
Ans:
True
Q.8
Predict the
output –
Ans: TypeError:
'tuple' object does not support item assignment
Skill Based Questions
Q.1
WAP that creates a tuple storing first 9
items of Fibonacci Series.
Ans:
Q.2 WAP that creates a third
tuple after adding two tuples. Add second after first tuple.
Ans:
Q.3 WAP to calculate the mean of the
numbers of the tuple.
Ans:
Q.4
WAP to calculate the average of the
numbers of the tuple.
Ans:
0 comments:
Post a Comment
Thanks for leaving a comment. As soon as it's approved, it will appear.