195 views

3 Answers

0 0 votes

C. [3,4,5,9,10, None, None]

extend() : Adds all elements of another list to the current list, returns None
append() : Adds a single element to the list, returns None

s.extend([4,5]) leaves us with [3,4,5]

s.append() calls inside the next .extend are evaluated first, that add 9, 10 to s in the same order, while returning None each time.

This creates a list [None, None] to be extended to s. 

$\therefore$ Answer is C. [3,4,5,9,10, None, None]. 

 

0 0 votes

Initially:

s = [3]

After:

s.extend([4, 5])

The list becomes:

[3, 4, 5]

Now observe this line carefully:

s.extend([s.append(9), s.append(10)])

Python first evaluates the list inside $\texttt{extend}$.

s.append(9)

This changes $\texttt{s}$ to:

[3, 4, 5, 9]

But $\texttt{append}$ returns $\texttt{None}$.

Then:

s.append(10)

This changes s to:

[3, 4, 5, 9, 10]

Again, $\texttt{append}$ returns $\texttt{None}$.

So finally:

s.extend([None, None])

Therefore, the output is:

[3, 4, 5, 9, 10, None, None]
Answer:
Position:
Show:

Related questions

2 2 votes
3 3 answers
243
243 views
GO Classes asked Jul 2
243 views
A function $\texttt{shuffle(s)}$ takes a sequence $\texttt{s}$ with an even number of elements. It returns a new list by interleaving the first half of $\texttt{s}$ with ...
2 2 votes
3 3 answers
219
219 views
GO Classes asked Jul 2
219 views
Consider the following Python code:s = [9, 7, 8] a, b = s, s[:] print(a is s, b == s, b is s) print(a.pop()) print(a + b)What is the output of the code above?True True Tr...
2 2 votes
2 2 answers
256
256 views
GO Classes asked Jul 2
256 views
A function $\texttt{deep_map(f, s)}$ replaces every non-list element $\texttt{x}$ inside a nested list $\texttt{​​​​​​​s}$ with $\texttt{f(x)}$.It modifies $\texttt{​​​​​...
2 2 votes
3 3 answers
259
259 views
GO Classes asked Jul 2
259 views
Consider the following Python code:s = [6, 7, 8] print(s.append(6)) s.insert(0, 9) x = s.pop(1) s.remove(x) print(s)What is the output of the code above?None [9, 7, 8]Non...