What Is the Output of This Python Code?
Question :
x = [10, 20, 30]
x.insert(1, 15)
print(x)
Options :
A) [10, 15, 20, 30]
B) [15, 10, 20, 30]
C) [10, 20, 15, 30]
D) Error
Correct Answer :
A) [10, 15, 20, 30]
Explanation :
first we create a list :
x = [10, 20, 30]
The list contains three elements.
Index : 0 1 2
Value :10 20 30
step 1 - insert ( )
x.insert(1, 15)
insert (index , value ) adds an element at a specific position in the list.
Here :
1 → position where the value will be inserted
15 → value to insert
So 15 is inserted at index 1.
The list becomes :
[10, 15, 20, 30]
Step 2 - print ( )
print(x)
Output :
[10, 15, 20, 30]
Practice daily Python coding challenges to improve your programming and logical thinking skills.
Happy coding ❤️

No comments:
Post a Comment