Python coding challenge - Day 1 | Output Prediction MCQ

 What Is the Output of This Python Code?


Python coding challenge - Day 1


 Question :  

x = [1, 2, 3]

x.append([4, 5])

print(len(x))


Options :

A) 3

B) 4

C) 5

D) Error


Correct Answer :

B) 4


Explanation :

first we create a list :

x = [1, 2, 3]

The list has 3 elements .


Step 1 - append( )

Now look at this line :

x.append([4, 5])

append( ) adds one element to the end of the list.

Improtant point :

append( )  adds the entire object as a single element.

so [4, 5] is added as one item not two items.


Step 2 - New List

The list becomes :

[1, 2, 3, [4, 5]]

Elements are now :

1 → 1
2 → 2
3 → 3
4 → [4, 5]

Total elements = 4


Step 3 - len( )

len(x)

len( ) counts the number of elements in the list.


So :

len (x) = 4


Output :

   4


Practice daily Python coding challenges to improve your programming and logical thinking skills.