Exercise: Data Types

Questions for: Data Types

What is the output of the following code snippet?
my_tuple = (1, 2, 3, 4, 5)
result = my_tuple.index(3)
print(result)
A:
2
B:
3
C:
4
D:
Error
Answer: A
The index() method in Python returns the index of the first occurrence of the specified element in the tuple. In this case, the element 3 is at index 2 in the tuple my_tuple, so 2 will be printed.
What is the output of the following code snippet?
my_set = {1, 2, 3, 4, 5}
result = my_set.discard(3)
print(result)
A:
{1, 2, 3, 5}
B:
{1, 2, 4, 5}
C:
None
D:
Error
Answer: C
The discard() method removes the specified element from the set but does not return a value (None).
How do you check if a variable is of type tuple?
A:
is_tuple(variable)
B:
type(variable) == tuple
C:
variable.is_tuple()
D:
tuple(variable)
Answer: B
The type() function is used to check the type of a variable.
What will be the output of the following code snippet?
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = len(my_dict)
print(result)
A:
{'a': 1, 'b': 2, 'c': 3}
B:
3
C:
6
D:
Error
Answer: B
The len() function returns the number of items (key-value pairs) in the dictionary.
What is the output of the following code snippet?
my_list = [1, 2, 3, 4, 5]
result = sum(my_list)
print(result)
A:
15
B:
12345
C:
5
D:
Error
Answer: A
The sum() function calculates the sum of all elements in a list.
Ad Slot (Above Pagination)
Quiz