Tuples Type A

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Tuples in Python

Type A Questions

Question 1: Discuss the utility and significance of tuples, briefly.


Answer:
Tuples are used to store multiple items in a single variable. It is a
collection which is ordered and immutable i.e., the elements of the
tuple can't be changed in place. Tuples are useful when values to be
stored are constant and need to be accessed quickly.

Question 2: If a is (1, 2, 3)

1. What is the difference (if any) between a * 3 and (a, a, a) ?


2. Is a * 3 equivalent to a + a + a?
3. What is the meaning of a[1:1] ?
4. What is the difference between a[1:2] and a[1:1] ?

Answer:

1. a * 3 ⇒ (1, 2, 3, 1, 2, 3, 1, 2, 3)
(a, a, a) ⇒ ((1, 2, 3), (1, 2, 3), (1, 2, 3))
So, a * 3 repeats the elements of the tuple whereas (a, a, a)
creates nested tuple.
2. Yes, both a * 3 and a + a + a will result in (1, 2, 3, 1, 2, 3, 1, 2, 3).
3. This colon indicates (:) simple slicing operator. Tuple slicing is
basically used to obtain a range of items.
tuple[Start : Stop] ⇒ returns the portion of the tuple from
index Start to index Stop (excluding element at stop).
a[1:1] ⇒ This will return empty tuple as a slice from index 1 to
index 0 is an invalid range.
4. Both are creating tuple slice with elements falling between
indexes start and stop.
a[1:2] ⇒ (2,)
It will return elements from index 1 to index 2 (excluding
element at 2).
a[1:1] ⇒ ()
a[1:1] specifies an invalid range as start and stop indexes are
the same. Hence, it will return an empty list.

Question 3: Does the slice operator always produce a new tuple?


Answer
No, the slice operator does not always produce a new tuple. If the
slice operator is applied on a tuple and the result is the same tuple,
then it will not produce a new tuple, it will return the same tuple as
shown in the example below:
a = (1, 2, 3)
print(a[:])
Slicing tuple a using a[:] results in the same tuple. Hence, in this case,
slice operator will not create a new tuple. Instead, it will return the
original tuple a.

Question 4: The syntax for a tuple with a single item is simply the
element enclosed in a pair of matching parentheses as shown
below:
t = ("a")
Is the above statement true? Why? Why not?
Answer
The statement is false. Single item tuple is always represented by
adding a comma after the item. If it is not added then python will
consider it as a string.
For example:
t1 = ("a",)
print(type(t1)) ⇒ tuple
t = ("a")
print(type(t)) ⇒ string
Question 5: Are the following two assignments same ? Why / why
not ?
1.
T1 = 3, 4, 5
T2 = ( 3, 4 , 5)
2.
T3 = (3, 4, 5)
T4 = (( 3, 4, 5))
Answer

1. T1 and T2 are same. Both are tuples. We can exclude/include


the parentheses when creating a tuple with multiple values.
2. T3 and T4 are not same. T3 is a tuple where as T4 is a nested
tuple.

Question 6: What would following statements print? Given that we


have tuple= ('t', 'p', 'l')

1. print("tuple")
2. print(tuple("tuple"))
3. print(tuple)

Answer

1. print("tuple") ⇒ tuple
It will simply print the item inside the print statement as it is of
string type.
2. print(tuple("tuple")) ⇒ it will throw error.
TypeError: 'tuple' object is not callable
This is because the variable "tuple" is being used to define a
tuple, and then is being used again as if it were a function. This
causes python to throw the error as now we are using tuple
object as a function but it is already defined as a tuple.
3. print(tuple) ⇒ ('t', 'p', 'l')
It will return the actual value of tuple.
Question 7: How is an empty tuple created?
Answer
There are two ways of creating an empty tuple:

1. By giving no elements in parentheses in assignment statement.


Example:
emptyTuple = ()
2. By using the tuple function.
Example:
emptyTuple = tuple()

Question 8: How is a tuple containing just one element created?


Answer
There are two ways of creating single element tuple:

1. By enclosing the element in parentheses and adding a comma


after it.
Example: t = (a,)
2. By using the built-in tuple type object (tuple( )) to create tuples
from sequences:
Example:
t = tuple([1])
Here, we pass a single element list to the tuple function and get
back a single element tuple.

Question 9: How can you add an extra element to a tuple?


Answer
We can use the concatenation operator to add an extra element to a
tuple as shown below. As tuples are immutable so they cannot be
modified in place.
For example:
t=(1,2,3)
t_append = t + (4,)
print(t)
print(t_append)
Output:
(1,2,3)
(1,2,3,4)

Question 10: When would you prefer tuples over lists?


Answer
Tuples are preferred over lists in the following cases:

1. When we want to ensure that data is not changed accidentally.


Tuples being immutable do not allow any changes in its data.
2. When we want faster access to data that will not change as
tuples are faster than lists.
3. When we want to use the data as a key in a dictionary. Tuples
can be used as keys in a dictionary, but lists cannot.
4. When we want to use the data as an element of a set. Tuples
can be used as elements of a set, but lists cannot.

Question 11: What is the difference between (30) and (30,)?


Answer
a = (30) ⇒ It will be treated as an integer expression, hence a stores
an integer 30, not a tuple.
a = (30,) ⇒ It is considered as single element tuple since a comma is
added after the element to convert it into a tuple.

You might also like