Font size
WorksheetsTUPLES IN PYTHON VAISHALI LAMBA
Total questions: 43
Worksheet time: 46mins
Tuples in Python are mutable
False
True
How to create a single element tuple
(1,)
(1)
{1}
[1]
tuple([1])
Result of the program above is
('1', '2', '3', '4', '5')
(1, 2, 3, 4, 5)
12345
"12345"
What is the output of the program above
<class 'tuple'>
<class 'None'>
<class 'list'>
<class 'int'>
What is the output of the code above
(1, '2', 3, 4, 5)
syntax error
(1, 2, 3, 4, 5)
('1', '2', '3', '4', '5')
Pick the odd one in connection with collection data type
List
Tuple
Dictionary
Loop
Which of the following function is used to count the number of elements in a list ?
count( )
find( )
len( )
index( )
Let list1=[2,4,6,8,10] the print(list1[-2]) will result in
10
8
4
6
What is the use of type( ) function in python ?
To create a tuple
To know the type of an element in tuple
To know the data type of the Python object
To create a List
Which of the following statement is not correct
A list is mutable
A tuple is immutable
The append function is used to add an element
The extend function is used in tuples to add elements in a list
If list=[10,20,30,40,50] then list[2]=35 will result
[35,10,20,30,40,50]
[10,20,30,40,50,35]
[10,20,35,40,50]
[10,35,30,40,50]
If list=[17,23,41,10] then list.append(32) will result
[32,17,23,41,10]
[17,23,41,10,32]
[10,17,23,32,41]
[41,32,23,17,10]
Which of the following python function can be used to add more than one element within an existing list ?
append( )
append_more( )
extend( )
more( )
The keys in Python dictionary is specified by
=
;
+
:
Which of these is the correct code for creating a list of names?
nameList = John, Harry, Jesse, John, Harry, Harry
nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList = [John, Harry, Jesse, John, Harry, Harry]
List items have an index number. In the following list, which item has the index number of 3?
["John", "Harry", "Jesse", "John", "Harry", "Harry"]
"John"
"Harry"
"Jesse"
Which of these pieces of code would return the name "Harry" from the following list?
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList()
nameList[1]
NameList(4)
nameList["4"]
What would this code show on the screen?
7
6
5
4
An error
What sort of bracket is for lists?
(
<
}
[
For tuples and list which is correct?
List and tuples both are mutable.
List is mutable whereas tuples are immutable.
List and tuples both are immutable.
List is immutable whereas tuples are mutable
print (people[-1])
What would this result be?
Which is a correctly declared tuple?
a = ("orange", "yellow", "red")
a = "orange", "yellow", "red"
a = ["orange", "yellow", "red"]
a = "orangeyellowred"
Which line of code will give you an error? b = (4, 5, 6, 7, 8)
b[2]
b[0] = 1
b[:3]
b[-2]
How would you refer to 3 in the following tuple? c = ((7,5),(5,8),(0,-1),(4,3))
c[0][0]
c(3)
c[8]
c[3][1]
Which of the following will produce an error? e = (1,2,3)
e = (4, 5, 6)
e = e + 4
e = e + (4, 5, 6)
e = e + (4,)
What is the index of the following tuple: y = ()
0
1
There is none. It is an empty tuple
Can you have an empty tuple z = () or an empty string w = []?
yes
no
Given a = "Hello world" what does list(a) do?
creates a list separated by word (white spaces)
creates a list separated by character
nothing
creates a blank list named a
Given a = "Hello world" what does a.split() do?
creates a list separated by character
nothing
creates a list separated by word (white spaces)
splits the string into two strings
Can you have lists inside of tuples and vice versa?
Yes
No
Suppose you have the following tuple definition:
t = ('foo', 'bar', 'baz')
Which of the following statements replaces the second element ('bar') with the string 'qux':
t[1] = 'qux'
t(1) = 'qux'
t[1:1] = 'qux'
It’s a trick question—tuples can’t be modified.
Which of the following functions is used to count the number of elements in a list ?
count( )
find( )
len( )
index( )
Let list1=[2,4,6,8,10].
What will print(list1[-2]) result in?
-2
8
4
6
Which of the following statements is NOT correct?
A list is mutable
A tuple is immutable
The append function is used to add an element
The extend function adds an element to a tuple
What type of data is: a=[(1,1),(2,4),(3,9)]?
Array of tuples
List of tuples
Tuples of lists
Invalid type
If L1=[2, 4, 8, 16] and L2=[32, 64]
what will result from L1+L2?
[2,4,8,16,32,64]
[2,4,8,16][32,64]
[2,4,8,16],[32,64]
2468163264
If L1=[2,4,8,16] what is the result of print(16 in L1)
True
False
Let x= "Python", the output of
print(x[0])
print(x[-1])
print(x[-2])
P
N
O
p
O
n
t
h
y
P
n
o
1. Examples for list data type
[1,2,3,4,5]
['S','H','R','U','T','H','I']
{1:"a",2:"b",3:"c",7:"d"}
('A','B','C','D')
Write the Python code that prints out Captain Kirk’s email address from the employee dictionary below:
employee = { "id": 1701, "name": "James T Kirk",
"email": "jtkirk@starfleet.com","position": "captain"}
(a)
Which of the following is not a valid way to define this dictionary in Python:
d = dict([
('foo', 100),
('bar', 200),
('baz', 300)
])
d = {}
d['foo'] = 100
d['bar'] = 200
d['baz'] = 300
d = dict(foo=100, bar=200, baz=300)
d = {'foo': 100, 'bar': 200, 'baz': 300}
d = { ('foo', 100), ('bar', 200), ('baz', 300) }
What would be the output of the following code snippet?
a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
for key in a_dict:
print(key)
'blue'
'apple'
'dog'
color
fruit
pet
blue
apple
dog
'color'
'fruit'
'pet'
What would be the output of the following code snippet?
a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
d_items = a_dict.items()
print(d_items)
('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')
dict_items([('color'), ('fruit'), ('pet')])
[('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')]
dict_items([('color', 'blue'), ('fruit', 'apple'), ('pet', 'dog')])
