In: Computer Science
Given the following code fragment:
x = (1,2,3)
Match the syntax with the action:
value = x{0} |
(a) value is bound to the value 1 (b) SyntaxError: invalid syntax (c) TypeError: 'tuple' object is not callable |
value = x(0) |
(a) value is bound to the value 1 (b) SyntaxError: invalid syntax (c) TypeError: 'tuple' object is not callable |
value = x[0] |
(a) value is bound to the value 1 (b) SyntaxError: invalid syntax (c) TypeError: 'tuple' object is not callable |
x = (1,2,3)
value = x{0}
Above given is a tuple,
We can only access tuple elements by using square brackets
1)
value = x{0}
We cannot access a tuple using curly brackets. Given above is invalid syntax because curly braces are used to store dictionary values but not to access the values. So its functionality is invalid. Result in Syntax Error
Answer : B (SyntaxError: invalid syntax)
Compiled Output :
2)
value = x(0)
We cannot access a tuple using curve brackets. Given above is a type error. We can assume x(0) as a function but the given x named is to a variable. In that case it finds out to be Type Error
Answer : C (TypeError: 'tuple' object is not callable)
Compiled Output :
3)
value = x[0]
Given is correct syntax andd gives a display index 0 i.e, value = 1
Answer : A ( value is bound to the value 1)