Tuple
Integer
List
Both tuple and integer
count()
len()
index()
find()
What will be the output of the following Python expression?
x=24//6%3, 24//4//2 print(x)
(1,3)
(0,3)
(1,0)
(3,1)
What will be the output of the following Python code?
>>> a=(2,3,4) >>> sum(a,3)
Too many arguments for sum() method
The method sum() doesn’t exist for tuples
12
9
Is the following Python code valid?
>>> a=2,3,4,5 >>> a
Yes, 2 is printed
Yes, [2,3,4,5] is printed
No, too many values to unpack
Yes, (2,3,4,5) is printed
>>>t = (1, 2, 4, 3, 8, 9)
>>>[t[i] for i in range(0, len(t), 2)]
[2, 3, 9]
[1, 2, 4, 3, 8, 9]
[1, 4, 8]
(1, 4, 8)
list
set
tuple
dict
Error, tuple slicing doesn’t exist
[2,3]
(2,3,4)
(2,3)
What will be the output of the following?
print(sum(1,2,3))
error
6
1
3
Slicing
Concatenation
Deletion of elements
Iteration
>>> a,b,c=1,2,3 >>> a,b,c
Yes, [1,2,3] is printed
No, invalid syntax
Yes, (1,2,3) is printed
1 is printed
tuple1=(5,1,7,6,2)tuple1.pop(2)print(tuple1)
(5,1,6,2)
(5,1,7,6)
(5,1,7,6,2)
Error
Yes, list mutable and tuple immutable
No, list and tuple both are mutable
No, list and tuple both are in immutable
No, just opposite, list immutable and tuple mutable
>>>t1 = (1, 2, 4, 3)
>>>t2 = (1, 2, 3, 4)
>>>t1 < t2
True
False
None
empty_tuple = ()
empty_tuple = tuple()
empty_tuple = []
Both A and B
>>> a=(1,2,3,4) >>> del a
No because tuple is immutable
Yes, first element in the tuple is deleted
Yes, the entire tuple is deleted
No, invalid syntax for del method
>>> a=(0,1,2,3,4) >>> b=slice(0,2) >>> a[b]
Invalid syntax for slicing
[0,2]
(0,1)
(0,2)
>>> import collections >>> a=collections.namedtuple('a',['i','j']) >>> obj=a(i=4,j=7) >>> obj
a(i=4, j=7)
obj(i=4, j=7)
(4,7)
An exception is thrown
>>> a=(1,2) >>> b=(3,4) >>> c=a+b >>> c
(4,6)
(1,2,3,4)
Error as tuples are immutable
>>>my_tuple = (1, 2, 3, 4)
>>>my_tuple.append( (5, 6, 7) )
>>>print len(my_tuple)
2
5
Built in
Derived data
append()
insert()
remove()
>>> a,b=1,2,3
Yes, this is an example of tuple unpacking. a=1 and b=2
Yes, this is an example of tuple unpacking. a=(1,2) and b=3
Yes, this is an example of tuple unpacking. a=1 and b=(2,3)
(1)
(1,)
[1]
{1}
Array of tuples
List of tuples
Tuples of lists
Invalid type
What is the output of the following code ?
ms = ('A','D', 'H','U','N','I','C')print(ms[1:4])
(‘D’, ‘H’, ‘U’)
(‘A’, ‘D’, ‘H’,’U’,’N’,’I’,’C’)
(‘D’,’H’,’U’,’N’,’I’,’C’)
In Python, a tuple can contain only integers as its elements.
In Python, a tuple can contain only strings as its elements.
In Python, a tuple can contain both integers and strings as its elements.
In Python, a tuple can contain either string or integer but not both at a time.
print(t[3])
t[3] = 45
print(max(t))
print(len(t))
a=[(2,4),(1,2),(3,9)]a.sort()print(a)
[(1, 2), (2, 4), (3, 9)]
[(2,4),(1,2),(3,9)]
Error because tuples are immutable
Error, tuple has no sort attribute
List of tuple
Tuple of List
Array of Tuples
Invalid Type
What is the output of the following statement ?
print ((2, 4) + (1, 5))
(2 , 4), (4 , 5)
(3 , 9)
(2, 4, 1, 5)
Invalid Syntax
>>> a,b=6,7 >>> a,b=b,a >>> a,b
(6,7)
Invalid syntax
(7,6)
Nothing is printed
>>>t=(1,2,4,3)
>>>t[1:3]
(1, 2)
(1, 2, 4)
(2, 4)
(2, 4, 3)
d = {"john":40, "peter":45}
d["john"]
40
45
“john”
“peter”
What will be the output of the following code?
a=((0,2,3,4)[1:-2])print(a)
(3,)
(2, )
(0,)
>>> a=(1,2,(4,5)) >>> b=(1,2,(3,4)) >>> a<b
Error, < operator is not valid for tuples
Error, < operator is valid for tuples but not if there are sub-tuples
Tuples have structure; lists have an order
Tuples are homogeneous, lists are heterogeneous.
Tuples are immutable, lists are mutable.
All of them
>>>t[1:-1]
What is the output of the following?
print(max([1, 2, 3, 4.] [4, 5, 6], [7]))
[4,5, 6
[7]
[1, 2,3, 4]
7
t1=1,2,4
t1=(1,)
t1=tuple(“123”)
All of these
numberGames = {}
numberGames[(1,2,4)] = 8
numberGames[(4,2,1)] = 10
numberGames[(1,2)] = 12
sum = 0
for k in numberGames:
sum += numberGames[k]
print len(numberGames) + sum
30
24
33
>>> a=("Check")*3 >>> a
(‘Check’,’Check’,’Check’)
* Operator not valid for tuples
(‘CheckCheckCheck’)
Syntax error
[1, 2, 3]
(1, 2, 3)
{1, 2, 3}
{}
>>>t = (1, 2)
>>>2 * t
(1, 2, 1, 2)
[1, 2, 1, 2]
(1, 1, 2, 2)
[1, 1, 2, 2]