124 views
1 1 vote

Consider the following Python code:

t = ("GATE", 2027, "DA", 3.5)
single = ("AI",)

print(t[0], t[-2], t[1:3], len(single))

What is the output of the code above?

  1. GATE 2027 ('DA', 3.5) 2
  2. GATE DA (2027, 'DA') 1
  3. GATE DA [2027, 'DA'] 1
  4. GATE DA (2027, 'DA') 2

1 Answer

0 0 votes

The tuple is:

t = ("GATE", 2027, "DA", 3.5)

The expression $\texttt{t[0]}$ gives the first element.

t[0] = "GATE"

The expression $\texttt{t[-2]}$ gives the second element from the end.

t[-2] = "DA"

The slice $\texttt{t[1:3]}$ starts from index $\texttt{1}$ and stops before index $\texttt{3}$.

t[1:3] = (2027, 'DA')

A single-element tuple needs a comma.

single = ("AI",)

So $\texttt{len(single)}$ is $\texttt{1}$.

Therefore, the output is:

GATE DA (2027, 'DA') 1

Correct Option: B

Answer:
Position:
Show:

Related questions

1 1 vote
1 1 answer
99
99 views
GO Classes asked Jun 20
99 views
Consider the following Python code:def calc(a, b): a, b = b, a + b return a, b, a + b x, y, z = calc(2, 5) print(x, y, z)What is the output of the code above?5 7 122 5 75...
0 0 votes
1 1 answer
125
125 views
GO Classes asked Jun 20
125 views
Consider the following Python code:def process(*nums): f = lambda x, y: x + 2 * y pairs = zip(nums, nums[1:]) return tuple(f(a, b) for a, b in pairs) print(process(1, 3, ...
2 2 votes
1 1 answer
93
93 views
GO Classes asked Jun 20
93 views
Consider the following Python code:print("GO", "Classes", sep="-", end="|") print("GATE", "DA", sep=" ", end="!") print("Done")What is the output of the code above?GO Cla...
0 0 votes
1 1 answer
113
113 views
GO Classes asked Jun 20
113 views
Consider the following Python code:def func(a, b=5, c=10, d=20): print("a =", a, "b =", b, "c =", c, "d =", d) func(2, c=30) func(c=4, a=1, d=6)What is the output of the ...