quiz 4 Flashcards
What is the output after the following statements?
x = list(‘abcdefgh’)
print(x[::-1][1:4])
[‘g’,’ f’,’ e’]
What will be the result of the following code in Python:
x = { “a”: 1, “b”: 2, “c”: }
print(x)
Generates an error, because dictionary keys must have a value.
Which statement is illegal in Python?
a. x = {(1, ), (2, )}
b. y = {[1], [2]}
c. y = {1, 2}
d. All are legal.
b. y = {[1], [2]}
Which collection is immutable but could contain mutable elements?
tuple
Which statement is illegal in Python?
a. x = {1 : 2, 3 : 4}
b. x = {(1, ) : 2, (3, ) : 4}
c. x = {[1] : 2, [3] : 4}
d. x = {1 : [2], 3 : [4]}
c. x = {[1] : 2, [3] : 4}
What is the output after the following?
x = [3,2,1]
id_1 = id(x)
x.sort()
id_2 = id(x)
y = [3,2,1]
id_3 = id(y)
y = sorted(y)
id_4 = id(y)
print(id_1 == id_2, id_3 == id_4)
True False
Which of the following does not exist in Python?
a. list comprehension
b. tuple comprehension
c. set comprehension
d. dictionary comprehension
b. tuple comprehension
If x is a list like this:
x = [1, 2, 3, 4]
And we want x to have this value instead:
[1, 2, 3, 4, 5]
Which of the following statements will achieve this?
x.append(5)
What is the output of the following?
x = [3, 4, 1, 220, 1, 225, 1, 30]
print(x.index(1))
4
If x is a dictionary and I call x.items(), I will get back the key-value pairs as lists.
False