Skip to content
Exercise · Q1

Q.What will be the output of the following statements? i.
list1 = [12,32,65,26,80,10]  
list1.sort()  
print(list1)  
ii.
list1 = [12,32,65,26,80,10]  
sorted(list1)  
print(list1)  
iii.
list1 = [1,2,3,4,5,6,7,8,9,10]  
list1[::-2]  
list1[:3] + list1[3:]  
iv.
list1 = [1,2,3,4,5]  
list1[len(list1)-1]  

Tamil Nadu DgeTextbookSubjective· 3mImportance★★★★★est
42% · 11/26 Questions
✓ Free question

sort() mutates the list, sorted() returns a new one leaving the original untouched, and a slice written as a bare expression computes a value without changing the list or printing anything (only the interactive shell echoes it).

The idea. All four parts test one distinction: which operations modify a list, which merely return a value. list.sort() works in place; sorted(list) builds a new list; slicing ([::-2], [:3], [3:]) and indexing always produce values without touching the original.

i.

list1 = [12,32,65,26,80,10]
list1.sort()
print(list1)

sort() rearranges list1 itself in ascending order.

[10, 12, 26, 32, 65, 80]

ii.

list1 = [12,32,65,26,80,10]
sorted(list1)
print(list1)

sorted(list1) builds and returns a new sorted list — but the result is not assigned to anything, so it is thrown away. list1 is not modified.

[12, 32, 65, 26, 80, 10]

iii.

list1 = [1,2,3,4,5,6,7,8,9,10]
list1[::-2]
list1[:3] + list1[3:]

Evaluating the expressions:

ExpressionMeaningValue
list1[::-2]every 2nd element, traversing backwards from the end[10, 8, 6, 4, 2]
list1[:3] + list1[3:]first three elements joined with the rest[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Neither value is printed or stored, so run as a script these two lines display nothing and list1 stays [1,...,10]. Typed line-by-line in the interactive shell, Python echoes each value: [10, 8, 6, 4, 2] then [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].

iv.

list1 = [1,2,3,4,5]
list1[len(list1)-1]

len(list1) is 5, so this is list1[4] — the last element:

5

(Again an expression: echoed as 5 in the shell; a script would need print() to show it.)

Watch out

The classic exam trap here is ii: sorted() never modifies its argument. If you want to keep the sorted result you must assign it — list1 = sorted(list1). And list1[len(list1)-1] is exactly what list1[-1] abbreviates.

✓Final answer

i prints [10, 12, 26, 32, 65, 80]; ii prints [12, 32, 65, 26, 80, 10] because sorted() returns a new list without changing list1; iii's expressions evaluate to [10, 8, 6, 4, 2] and [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] but print nothing in a script (shell echoes them); iv evaluates to 5, the last element.

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.