96 views
2 2 votes

Consider the following Python code:

letters = ["A", "B", "C"]
numbers = [1, 2, 3]

pairs = list(zip(letters, numbers))

print(pairs[1], pairs[-1][0] + str(pairs[0][1]))

What is the output of the code above?

  1. ('A', 1) C1
  2. ('B', 2) C1
  3. ('B', 2) A3
  4. ['B', 2] C1

1 Answer

0 0 votes

The $\texttt{zip()}$ function pairs elements from the given iterables position-wise.

letters = ["A", "B", "C"]
numbers = [1, 2, 3]

So:

pairs = [('A', 1), ('B', 2), ('C', 3)]

Now $\texttt{pairs[1]}$ gives the second tuple.

pairs[1] = ('B', 2)

Also, $\texttt{pairs[-1]}$ gives the last tuple.

pairs[-1] = ('C', 3)
pairs[-1][0] = 'C'

And $\texttt{pairs[0][1]}$ gives $\texttt{1}$.

pairs[-1][0] + str(pairs[0][1]) = 'C' + '1' = 'C1'

Therefore, the output is:

('B', 2) C1

Correct Option: B

Answer:
Position:
Show:

Related questions

2 2 votes
1 1 answer
105
105 views
GO Classes asked Jun 22
105 views
Consider the following Python code:a = "GATE" b = [10, 20] c = range(3) z = list(zip(a, b, c)) print(z, len(z))What is the output of the code above?[('G', 10, 0), ('A', 2...
1 1 vote
1 1 answer
130
130 views
GO Classes asked Jun 22
130 views
Consider the following Python code:from functools import reduce nums = [2, 3, 4] ans = reduce(lambda acc, x: acc * x + 1, nums, 1) print(ans)What is the output of the cod...
1 1 vote
1 1 answer
118
118 views
GO Classes asked Jun 22
118 views
Consider the following Python code:nums = [1, 2, 3, 4, 5] mapped = list(map(lambda x: x * x if x % 2 else x + 10, nums)) filtered = list(filter(lambda x: x % 3 == 0 or x ...
0 0 votes
1 1 answer
99
99 views
GO Classes asked Jun 22
99 views
Consider the following Python code:def fun(a, *numbers): print(a, numbers, len(numbers)) return a + sum(numbers[:2]) print(fun(10, 20, 30, 40))What is the output of the c...