Exercise: Python

Questions for: Tuples

How can you check if two tuples are equal?
A:
tuple1.equals(tuple2)
B:
tuple1 == tuple2
C:
tuple1.compare(tuple2)
D:
tuple1 is tuple2
Answer: B
The '==' operator is used to check if two tuples are equal.
What is the result of the expression len(('apple',))?
A:
5
B:
1
C:
('apple',)
D:
Raises a TypeError
Answer: B
The len() function returns the number of elements in the tuple, which is 1 in this case.
What does the tuple() function do when given a list as an argument?
A:
Creates a tuple containing the entire list as a single element.
B:
Converts the list into a tuple.
C:
Raises a TypeError since lists and tuples are incompatible.
D:
Appends the list to an existing tuple.
Answer: B
The tuple() function can be used to convert a list into a tuple.
What is the result of the expression tuple((1, 2, 3)) * 3?
A:
(1, 2, 3, 1, 2, 3, 1, 2, 3)
B:
((1, 2, 3), (1, 2, 3), (1, 2, 3))
C:
[1, 2, 3, 1, 2, 3, 1, 2, 3]
D:
(1, 2, 3, (1, 2, 3), (1, 2, 3))
Answer: A
The '*' operator is used to repeat the elements of the tuple.
How do you convert a tuple to a string?
A:
str(tuple)
B:
tuple.toString()
C:
tuple.join('')
D:
string(tuple)
Answer: A
The str() function is used to convert a tuple to a string.
Ad Slot (Above Pagination)
Quiz