What is the output of the following code?
tup = (1, 2, 4, 8) tup = tup[1:-1] tup = tup[0] print(tup)
- (2)
- (2, )
- 2
- the snippet is erroneous
Explanation: Let’s analyze the code:
- a tuple named tup with the following elements is defined: (1, 2, 4, 8)
- the tuple tupis replaced with a shorter version of itself. The indices are [1:-1], which means it will start at position 1 to the second-last element of the tuple. The new tuple is (2, 4)
- the tuple tup is again replaced with its first element only: tup[0], and the result is no longer a tuple,
- the print function shows 2 on the console.
More Questions: Python Essentials 1 – Module 4 Test
Please login or Register to submit your answer