Exercise: Data Types

Questions for: Data Types

What will be the result of the following code snippet?
my_tuple = (1, 2, 3, 4, 5)
result = my_tuple[2:]
print(result)
A:
(3, 4, 5)
B:
(1, 2, 3)
C:
(1, 2)
D:
(2, 3, 4, 5)
Answer: A
Slicing from index 2 to the end includes elements at indices 2, 3, and 4.
What is the purpose of the set() function?
A:
Converts a list to a set
B:
Creates an empty set
C:
Removes an element from a set
D:
Checks if a variable is of a certain type
Answer: B
The set() function is used to create an empty set in Python.
How do you remove the last element from a Python list?
A:
list.remove(-1)
B:
list.pop()
C:
list.delete(-1)
D:
list.removeLast()
Answer: B
The pop() method without an index argument removes and returns the last element from the list.
How can you concatenate two dictionaries?
A:
dict1 + dict2
B:
dict1.append(dict2)
C:
dict1.update(dict2)
D:
concat(dict1, dict2)
Answer: C
The update() method is used to merge the keys and values of one dictionary into another.
What will be the result of the following code snippet?
my_list = [1, 2, 3, 4, 5]
result = my_list.pop(2)
print(result)
A:
2
B:
3
C:
4
D:
[1, 2, 4, 5]
Answer: B
The pop() method removes and returns the element at the specified index (2 in this case).
Ad Slot (Above Pagination)
Quiz