What is the output of the following Python code:
veggies = ['cabbage', 'brinjal', 'onion', 'beans']
veggies.insert(veggies.index('brinjal'), 'celery')
print(veggies)
[cabbage. 'celery 'brinjal', 'onion' 'beans']
[cabbage celery, 'onion', 'beans']
[cabbage. 'bringal', 'celery. 'Onion 'beans']
[celery, 'cabbage'brinjal, 'onion'beans')
0
Answers
Question:
What is the output of the following Python code:
veggies = ['cabbage', 'brinjal', 'onion', 'beans']
veggies.insert(veggies.index('brinjal'), 'celery')
print(veggies)
(A) ['cabbage', 'celery', 'brinjal', 'onion', 'beans']
(B) ['cabbage', 'celery', 'onion', 'beans']
(C) ['cabbage', 'brinjal', 'celery', 'onion', 'beans']
(D) ['celery', 'cabbage', 'brinjal', 'onion', 'beans']
Answer:
(A) ['cabbage', 'celery', 'brinjal', 'onion', 'beans'] is the correct answer.
In this code, 'celery' is inserted before 'brinjal' that is why 'brinjal' shifts one position to it's right side.
Extra Information:
In question 'veggies' (variable) is a list in which multiple items are stored.
insert() is a method in Python which inserts the specified value in specified position.
Python is created by Guido van Rossum and it came into existence in 1991. It is high-level and general programming language. It is a very lucid programming language as it has good language construct and object oriented approach. It is dynamically typed and garbage-collected.
Answer:
['cabbage', 'celery', 'brinjal', 'onion', 'beans'] is the correct answer.
Explanation: